




import inspect
import unittest

from transformers import RegNetConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
       import torch
       from torch import nn

       from transformers import RegNetForImageClassification, RegNetModel
       from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor





class     lowerCamelCase__:
      def __init__(				self: int    ,       UpperCamelCase_: Optional[int]    ,       UpperCamelCase_: Any=3    ,       UpperCamelCase_: Optional[Any]=32    ,       UpperCamelCase_: Union[str, Any]=3    ,       UpperCamelCase_: Dict=10    ,       UpperCamelCase_: Optional[Any]=[10, 20, 30, 40]    ,       UpperCamelCase_: List[Any]=[1, 1, 2, 1]    ,       UpperCamelCase_: Union[str, Any]=True    ,       UpperCamelCase_: Dict=True    ,       UpperCamelCase_: Any="relu"    ,       UpperCamelCase_: int=3    ,       UpperCamelCase_: Optional[int]=None    ,       ):
             __lowerCamelCase   =						parent
             __lowerCamelCase   =						batch_size
             __lowerCamelCase   =						image_size
             __lowerCamelCase   =						num_channels
             __lowerCamelCase   =						embeddings_size
             __lowerCamelCase   =						hidden_sizes
             __lowerCamelCase   =						depths
             __lowerCamelCase   =						is_training
             __lowerCamelCase   =						use_labels
             __lowerCamelCase   =						hidden_act
             __lowerCamelCase   =						num_labels
             __lowerCamelCase   =						scope
             __lowerCamelCase   =						len(_a   )
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

             __lowerCamelCase   =						None
             if self.use_labels:
                    __lowerCamelCase   =						ids_tensor([self.batch_size]    ,       self.num_labels   )

             __lowerCamelCase   =						self.get_config()

             return config, pixel_values, labels
      def 			lowerCAmelCase__						(				self: List[str]   ):
             return RegNetConfig(
                 num_channels=self.num_channels    ,       embeddings_size=self.embeddings_size    ,       hidden_sizes=self.hidden_sizes    ,       depths=self.depths    ,       hidden_act=self.hidden_act    ,       num_labels=self.num_labels    ,       )
      def 			lowerCAmelCase__						(				self: Optional[Any]    ,       UpperCamelCase_: str    ,       UpperCamelCase_: List[str]    ,       UpperCamelCase_: List[str]   ):
             __lowerCamelCase   =						RegNetModel(config=_a   )
             model.to(_a   )
             model.eval()
             __lowerCamelCase   =						model(_a   )
             # expected last hidden states: B, C, H // 32, W // 32
             self.parent.assertEqual(
                 result.last_hidden_state.shape    ,       (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,       )
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: List[Any]    ,       UpperCamelCase_: Any    ,       UpperCamelCase_: Optional[Any]   ):
             __lowerCamelCase   =						self.num_labels
             __lowerCamelCase   =						RegNetForImageClassification(_a   )
             model.to(_a   )
             model.eval()
             __lowerCamelCase   =						model(_a    ,       labels=_a   )
             self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.num_labels)   )




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





@require_torch
class     lowerCamelCase__(					A__ ,     A__ ,     unittest.TestCase):
      UpperCAmelCase__      :	Any    		=   (RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
      UpperCAmelCase__      :	Dict    		=   (
          {'feature-extraction': RegNetModel, 'image-classification': RegNetForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCAmelCase__      :	List[str]    		=   False
      UpperCAmelCase__      :	Any    		=   False
      UpperCAmelCase__      :	Tuple    		=   False
      UpperCAmelCase__      :	Optional[Any]    		=   False
      def 			lowerCAmelCase__						(				self: Dict   ):
             __lowerCamelCase   =						RegNetModelTester(self   )
             __lowerCamelCase   =						ConfigTester(self    ,       config_class=_a    ,       has_text_modality=_a   )
      def 			lowerCAmelCase__						(				self: int   ):
             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: Tuple   ):
             return
      @unittest.skip(reason="""RegNet does not use inputs_embeds"""   )
      def 			lowerCAmelCase__						(				self: Tuple   ):
             pass
      @unittest.skip(reason="""RegNet does not support input and output embeddings"""   )
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             pass
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase,   __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs_for_common()

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

                    __lowerCamelCase   =						["""pixel_values"""]
                    self.assertListEqual(arg_names[:1]    ,       _a   )
      def 			lowerCAmelCase__						(				self: str   ):
             __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_a   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             __lowerCamelCase,   __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    __lowerCamelCase   =						model_class(config=_a   )
                    for name, module in model.named_modules():
                           if isinstance(_a    ,       (nn.BatchNormad, nn.GroupNorm)   ):
                                  self.assertTrue(
                                      torch.all(module.weight == 1   )    ,       msg=F'Parameter {name} of model {model_class} seems not properly initialized'    ,       )
                                  self.assertTrue(
                                      torch.all(module.bias == 0   )    ,       msg=F'Parameter {name} of model {model_class} seems not properly initialized'    ,       )
      def 			lowerCAmelCase__						(				self: int   ):






             def check_hidden_states_output(UpperCamelCase_: Optional[int]    ,       UpperCamelCase_: int    ,       UpperCamelCase_: Dict   ):
                    __lowerCamelCase   =						model_class(_a   )
                    model.to(_a   )
                    model.eval()

                    with torch.no_grad():
                           __lowerCamelCase   =						model(**self._prepare_for_class(_a    ,       _a   )   )

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

                    __lowerCamelCase   =						self.model_tester.num_stages
                    self.assertEqual(len(_a   )    ,       expected_num_stages + 1   )

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

             __lowerCamelCase,   __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs_for_common()
             __lowerCamelCase   =						["""basic""", """bottleneck"""]
             for model_class in self.all_model_classes:
                    for layer_type in layers_type:
                           __lowerCamelCase   =						layer_type
                           __lowerCamelCase   =						True
                           check_hidden_states_output(_a    ,       _a    ,       _a   )

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

                           check_hidden_states_output(_a    ,       _a    ,       _a   )
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*_a   )




      @slow
      def 			lowerCAmelCase__						(				self: List[str]   ):






             for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    __lowerCamelCase   =						RegNetModel.from_pretrained(_a   )
                    self.assertIsNotNone(_a   )




def 					lowerCamelCase__       (						):





       '''simple docstring'''



       __lowerCamelCase   =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
       return image





@require_torch
@require_vision
class     lowerCamelCase__(					unittest.TestCase):
      @cached_property
      def 			lowerCAmelCase__						(				self: Optional[Any]   ):
             return (
                 AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )
                 if is_vision_available()
                 else None
             )




      @slow
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]   ).to(_a   )

             __lowerCamelCase   =						self.default_image_processor
             __lowerCamelCase   =						prepare_img()
             __lowerCamelCase   =						image_processor(images=_a    ,       return_tensors="""pt"""   ).to(_a   )

             # forward pass
             with torch.no_grad():
                    __lowerCamelCase   =						model(**_a   )

             # verify the logits
             __lowerCamelCase   =						torch.Size((1, 10_00)   )
             self.assertEqual(outputs.logits.shape    ,       _a   )

             __lowerCamelCase   =						torch.tensor([-0.4180, -1.5051, -3.4836]   ).to(_a   )

             self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,       _a    ,       atol=1E-4   )   )




'''simple docstring'''
from typing import List, Optional, Union

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


lowerCamelCase :      List[Any]					 =     logging.get_logger(__name__)

lowerCamelCase :      str					 =     {
    "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 							A__    (       A__  ):
  A__		 =				'time_series_transformer'
  A__		 =				{
      'hidden_size': 'd_model',
      'num_attention_heads': 'encoder_attention_heads',
      'num_hidden_layers': 'encoder_layers',
  }





  def __init__(		self      :	Optional[int]							,					_a      :	Optional[int] = None							,					_a      :	Optional[int] = None							,					_a      :	str = "student_t"							,					_a      :	str = "nll"							,					_a      :	int = 1							,					_a      :	List[int] = [1, 2, 3, 4, 5, 6, 7]							,					_a      :	Optional[Union[str, bool]] = "mean"							,					_a      :	int = 0							,					_a      :	int = 0							,					_a      :	int = 0							,					_a      :	int = 0							,					_a      :	Optional[List[int]] = None							,					_a      :	Optional[List[int]] = None							,					_a      :	int = 32							,					_a      :	int = 32							,					_a      :	int = 2							,					_a      :	int = 2							,					_a      :	int = 2							,					_a      :	int = 2							,					_a      :	bool = True							,					_a      :	str = "gelu"							,					_a      :	int = 64							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	float = 0.1							,					_a      :	int = 100							,					_a      :	float = 0.02							,					_a      :	Union[str, Any]=True							,					**_a      :	Optional[Any]							,					)	->    Optional[Any]:





       '''simple docstring'''






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

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

       _SCREAMING_SNAKE_CASE							    =dropout
       _SCREAMING_SNAKE_CASE							    =attention_dropout
       _SCREAMING_SNAKE_CASE							    =activation_dropout
       _SCREAMING_SNAKE_CASE							    =encoder_layerdrop
       _SCREAMING_SNAKE_CASE							    =decoder_layerdrop

       _SCREAMING_SNAKE_CASE							    =activation_function
       _SCREAMING_SNAKE_CASE							    =init_std

       _SCREAMING_SNAKE_CASE							    =use_cache

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





  @property
  def 						A  (		self      :	List[Any] )	->    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
       )
