


import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, require_torch, 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


UpperCamelCase					:       int			    =     1E-4

if is_torch_available():
    import torch

    from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
    from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder



@require_torch
class A__     :



  """simple docstring"""



  def __init__( self			:  Optional[int]				,				lowerCamelCase__			:  Tuple				,				lowerCamelCase__			:  Optional[Any]=16				,				lowerCamelCase__			:  Dict=13				,				lowerCamelCase__			:  Dict=7				,				lowerCamelCase__			:  List[Any]=14				,				lowerCamelCase__			:  Optional[int]=10				,				lowerCamelCase__			:  int=19				,				lowerCamelCase__			:  List[Any]=5				,				lowerCamelCase__			:  int=4				,				lowerCamelCase__			:  str=True				,				lowerCamelCase__			:  Any=16				,				lowerCamelCase__			:  Union[str, Any]=2				,				lowerCamelCase__			:  int=4				,				lowerCamelCase__			:  Tuple=4				,				lowerCamelCase__			:  Dict="gelu"				,				lowerCamelCase__			:  List[Any]=0.1				,				lowerCamelCase__			:  List[Any]=0.1				,				lowerCamelCase__			:  Any=[1, 2, 3, 4, 5]				,				lowerCamelCase__			:  List[Any]=25				,				lowerCamelCase__			:  List[Any]=5				,				):
        a__	:  Optional[Any]     =   d_model
        a__	:  List[str]     =   parent
        a__	:  str     =   batch_size
        a__	:  List[Any]     =   prediction_length
        a__	:  List[Any]     =   context_length
        a__	:  str     =   cardinality
        a__	:  List[Any]     =   num_time_features
        a__	:  Dict     =   lags_sequence
        a__	:  int     =   embedding_dimension
        a__	:  Dict     =   is_training
        a__	:  Dict     =   hidden_size
        a__	:  int     =   num_hidden_layers
        a__	:  str     =   num_attention_heads
        a__	:  Dict     =   intermediate_size
        a__	:  List[str]     =   hidden_act
        a__	:  str     =   hidden_dropout_prob
        a__	:  Optional[Any]     =   attention_probs_dropout_prob

        a__	:  Optional[int]     =   context_length
        a__	:  Any     =   prediction_length + label_length
        a__	:  List[Any]     =   label_length

        a__	:  int     =   moving_average
        a__	:  Any     =   autocorrelation_factor

  def    _UpperCamelCase( self			:  Dict       ):
        return AutoformerConfig(
            d_model=self.d_model				,				encoder_layers=self.num_hidden_layers				,				decoder_layers=self.num_hidden_layers				,				encoder_attention_heads=self.num_attention_heads				,				decoder_attention_heads=self.num_attention_heads				,				encoder_ffn_dim=self.intermediate_size				,				decoder_ffn_dim=self.intermediate_size				,				dropout=self.hidden_dropout_prob				,				attention_dropout=self.attention_probs_dropout_prob				,				prediction_length=self.prediction_length				,				context_length=self.context_length				,				label_length=self.label_length				,				lags_sequence=self.lags_sequence				,				num_time_features=self.num_time_features				,				num_static_categorical_features=1				,				cardinality=[self.cardinality]				,				embedding_dimension=[self.embedding_dimension]				,				moving_average=self.moving_average				,				)

  def    _UpperCamelCase( self			:  int				,				lowerCamelCase__			:  List[Any]       ):
        a__	:  str     =   config.context_length + max(config.lags_sequence       )

        a__	:  Optional[int]     =   ids_tensor([self.batch_size, 1]				,				config.cardinality[0]       )
        a__	:  Optional[Any]     =   floats_tensor([self.batch_size, _past_length, config.num_time_features]       )
        a__	:  Optional[int]     =   floats_tensor([self.batch_size, _past_length]       )
        a__	:  Any     =   floats_tensor([self.batch_size, _past_length]       ) > 0.5

        # decoder inputs
        a__	:  Optional[Any]     =   floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]       )
        a__	:  Union[str, Any]     =   floats_tensor([self.batch_size, config.prediction_length]       )

        a__	:  str     =   {
            "past_values": past_values,
            "static_categorical_features": static_categorical_features,
            "past_time_features": past_time_features,
            "past_observed_mask": past_observed_mask,
            "future_time_features": future_time_features,
            "future_values": future_values,
        }
        return inputs_dict

  def    _UpperCamelCase( self			:  List[Any]       ):
        a__	:  Dict     =   self.get_config()
        a__	:  Any     =   self.prepare_autoformer_inputs_dict(lowerCamelCase__       )
        return config, inputs_dict

  def    _UpperCamelCase( self			:  str       ):
        a__,	a__	:  Tuple     =   self.prepare_config_and_inputs()
        return config, inputs_dict

  def    _UpperCamelCase( self			:  Tuple				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Optional[Any]       ):
        a__	:  Union[str, Any]     =   AutoformerModel(config=lowerCamelCase__       ).to(lowerCamelCase__       ).eval()
        a__	:  List[Any]     =   model(**lowerCamelCase__       )

        a__	:  int     =   outputs.encoder_last_hidden_state
        a__	:  Optional[Any]     =   outputs.last_hidden_state

        with tempfile.TemporaryDirectory() as tmpdirname:
              a__	:  str     =   model.get_encoder()
              encoder.save_pretrained(lowerCamelCase__       )
              a__	:  List[str]     =   AutoformerEncoder.from_pretrained(lowerCamelCase__       ).to(lowerCamelCase__       )

        a__,	a__,	a__,	a__,	a__	:  List[str]     =   model.create_network_inputs(**lowerCamelCase__       )
        a__,	a__	:  Union[str, Any]     =   model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]       )

        a__	:  str     =   torch.cat(
            (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])				,				dim=-1				,				)
        a__	:  Optional[int]     =   encoder(inputs_embeds=lowerCamelCase__       )[0]
        self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3       )

        a__	:  Tuple     =   (
            torch.mean(transformer_inputs[:, : config.context_length, ...]				,				dim=1       )
            .unsqueeze(1       )
            .repeat(1				,				config.prediction_length				,				1       )
        )
        a__	:  Tuple     =   torch.zeros(
            [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]				,				device=enc_input.device				,				)

        a__	:  Dict     =   torch.cat(
            (
                torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)				,				dim=1       ),
                feature[:, config.context_length - config.label_length :, ...],
            )				,				dim=-1				,				)
        a__	:  Union[str, Any]     =   torch.cat(
            (
                torch.cat((trend_input[:, -config.label_length :, ...], mean)				,				dim=1       ),
                feature[:, config.context_length - config.label_length :, ...],
            )				,				dim=-1				,				)

        with tempfile.TemporaryDirectory() as tmpdirname:
              a__	:  Any     =   model.get_decoder()
              decoder.save_pretrained(lowerCamelCase__       )
              a__	:  Dict     =   AutoformerDecoder.from_pretrained(lowerCamelCase__       ).to(lowerCamelCase__       )

        a__	:  Optional[int]     =   decoder(
            trend=lowerCamelCase__				,				inputs_embeds=lowerCamelCase__				,				encoder_hidden_states=lowerCamelCase__				,				)[0]

        self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3       )




@require_torch
class A__     ( A__				,		A__				,		unittest.TestCase  ):



  """simple docstring"""



  _lowercase									=		(AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
  _lowercase									=		(AutoformerForPrediction,) if is_torch_available() else ()
  _lowercase									=		{'feature-extraction': AutoformerModel} if is_torch_available() else {}
  _lowercase									=		False
  _lowercase									=		False
  _lowercase									=		False
  _lowercase									=		False
  _lowercase									=		False
  _lowercase									=		False

  def    _UpperCamelCase( self			:  Optional[Any]       ):
        a__	:  List[Any]     =   AutoformerModelTester(self       )
        a__	:  List[Any]     =   ConfigTester(self				,				config_class=lowerCamelCase__				,				has_text_modality=lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Optional[Any]       ):
        self.config_tester.run_common_tests()

  def    _UpperCamelCase( self			:  List[Any]       ):
        a__,	a__	:  int     =   self.model_tester.prepare_config_and_inputs()
        for model_class in self.all_model_classes:
              a__	:  Any     =   model_class(lowerCamelCase__       )

              with tempfile.TemporaryDirectory() as tmpdirname:
                    model.save_pretrained(lowerCamelCase__       )
                    a__,	a__	:  List[str]     =   model_class.from_pretrained(lowerCamelCase__				,				output_loading_info=lowerCamelCase__       )
              self.assertEqual(info["missing_keys"]				,				[]       )

  def    _UpperCamelCase( self			:  Union[str, Any]       ):
        a__	:  List[Any]     =   self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_encoder_decoder_model_standalone(*lowerCamelCase__       )

  @unittest.skip(reason="Model has no tokens embeddings"       )
  def    _UpperCamelCase( self			:  Optional[Any]       ):
        pass

  def    _UpperCamelCase( self			:  Any       ):
        a__	:  str     =   inspect.signature(getattr(lowerCamelCase__				,				"forward"       )       )
        # The main input is the name of the argument after `self`
        a__	:  int     =   list(model_signature.parameters.keys()       )[1]
        self.assertEqual(AutoformerModel.main_input_name				,				lowerCamelCase__       )

  def    _UpperCamelCase( self			:  int       ):
        a__,	a__	:  Union[str, Any]     =   self.model_tester.prepare_config_and_inputs_for_common()

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

              a__	:  Any     =   [
                  "past_values",
                  "past_time_features",
                  "past_observed_mask",
                  "static_categorical_features",
                  "static_real_features",
                  "future_values",
                  "future_time_features",
              ]

              if model.__class__.__name__ in ["AutoformerForPrediction"]:
                    expected_arg_names.append("future_observed_mask"       )

              expected_arg_names.extend(
                  [
                      "decoder_attention_mask",
                      "head_mask",
                      "decoder_head_mask",
                      "cross_attn_head_mask",
                      "encoder_outputs",
                      "past_key_values",
                      "output_hidden_states",
                      "output_attentions",
                      "use_cache",
                      "return_dict",
                  ]       )

              self.assertListEqual(arg_names[: len(lowerCamelCase__       )]				,				lowerCamelCase__       )

  def    _UpperCamelCase( self			:  List[Any]       ):
        a__,	a__	:  Optional[int]     =   self.model_tester.prepare_config_and_inputs_for_common()
        a__	:  Any     =   True

        a__	:  int     =   getattr(self.model_tester				,				"seq_length"				,				lowerCamelCase__       )
        a__	:  Optional[Any]     =   getattr(self.model_tester				,				"decoder_seq_length"				,				lowerCamelCase__       )
        a__	:  Optional[int]     =   getattr(self.model_tester				,				"encoder_seq_length"				,				lowerCamelCase__       )
        a__	:  Optional[int]     =   getattr(self.model_tester				,				"d_model"				,				lowerCamelCase__       )
        a__	:  Tuple     =   getattr(self.model_tester				,				"num_attention_heads"				,				lowerCamelCase__       )
        a__	:  Union[str, Any]     =   d_model // num_attention_heads

        for model_class in self.all_model_classes:
              a__	:  Tuple     =   True
              a__	:  List[Any]     =   False
              a__	:  Optional[Any]     =   True
              a__	:  Optional[Any]     =   model_class(lowerCamelCase__       )
              model.to(lowerCamelCase__       )
              model.eval()
              with torch.no_grad():
                    a__	:  str     =   model(**self._prepare_for_class(lowerCamelCase__				,				lowerCamelCase__       )       )
              a__	:  Union[str, Any]     =   outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
              self.assertEqual(len(lowerCamelCase__       )				,				self.model_tester.num_hidden_layers       )

              # check that output_attentions also work using config
              del inputs_dict["output_attentions"]
              a__	:  List[str]     =   True
              a__	:  Any     =   model_class(lowerCamelCase__       )
              model.to(lowerCamelCase__       )
              model.eval()
              with torch.no_grad():
                    a__	:  str     =   model(**self._prepare_for_class(lowerCamelCase__				,				lowerCamelCase__       )       )
              a__	:  str     =   outputs.encoder_attentions
              self.assertEqual(len(lowerCamelCase__       )				,				self.model_tester.num_hidden_layers       )

              self.assertListEqual(
                  list(attentions[0].shape[-3:]       )				,				[self.model_tester.num_attention_heads, encoder_seq_length, dim]				,				)
              a__	:  int     =   len(lowerCamelCase__       )

              a__	:  Tuple     =   7

              if "last_hidden_state" in outputs:
                    correct_outlen += 1

              if "trend" in outputs:
                    correct_outlen += 1

              if "past_key_values" in outputs:
                    correct_outlen += 1  # past_key_values have been returned

              if "loss" in outputs:
                    correct_outlen += 1

              if "params" in outputs:
                    correct_outlen += 1

              self.assertEqual(lowerCamelCase__				,				lowerCamelCase__       )

              # decoder attentions
              a__	:  List[Any]     =   outputs.decoder_attentions
              self.assertIsInstance(lowerCamelCase__				,				(list, tuple)       )
              self.assertEqual(len(lowerCamelCase__       )				,				self.model_tester.num_hidden_layers       )
              self.assertListEqual(
                  list(decoder_attentions[0].shape[-3:]       )				,				[self.model_tester.num_attention_heads, decoder_seq_length, dim]				,				)

              # cross attentions
              a__	:  Optional[int]     =   outputs.cross_attentions
              self.assertIsInstance(lowerCamelCase__				,				(list, tuple)       )
              self.assertEqual(len(lowerCamelCase__       )				,				self.model_tester.num_hidden_layers       )
              self.assertListEqual(
                  list(cross_attentions[0].shape[-3:]       )				,				[self.model_tester.num_attention_heads, decoder_seq_length, dim]				,				)

        # Check attention is always last and order is fine
        a__	:  int     =   True
        a__	:  List[str]     =   True
        a__	:  Union[str, Any]     =   model_class(lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        with torch.no_grad():
              a__	:  Optional[Any]     =   model(**self._prepare_for_class(lowerCamelCase__				,				lowerCamelCase__       )       )

        self.assertEqual(out_len + 2				,				len(lowerCamelCase__       )       )

        a__	:  str     =   outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

        self.assertEqual(len(lowerCamelCase__       )				,				self.model_tester.num_hidden_layers       )
        self.assertListEqual(
            list(self_attentions[0].shape[-3:]       )				,				[self.model_tester.num_attention_heads, encoder_seq_length, dim]				,				)

  @is_flaky()
  def    _UpperCamelCase( self			:  Any       ):
        super().test_retain_grad_hidden_states_attentions()




def UpperCamelCase_				(				__a="train-batch.pt"      )	->   Optional[Any]:
      a__	:  Union[str, Any]     =   hf_hub_download(repo_id="hf-internal-testing/tourism-monthly-batch"			,				filename=__a			,				repo_type="dataset"      )
      a__	:  Tuple     =   torch.load(__a			,				map_location=__a      )
      return batch



@require_torch
@slow
class A__     ( unittest.TestCase  ):



  """simple docstring"""



  def    _UpperCamelCase( self			:  Tuple       ):
        a__	:  Optional[Any]     =   AutoformerModel.from_pretrained("huggingface/autoformer-tourism-monthly"       ).to(lowerCamelCase__       )
        a__	:  Optional[Any]     =   prepare_batch()

        with torch.no_grad():
              a__	:  int     =   model(
                  past_values=batch["past_values"]				,				past_time_features=batch["past_time_features"]				,				past_observed_mask=batch["past_observed_mask"]				,				static_categorical_features=batch["static_categorical_features"]				,				future_values=batch["future_values"]				,				future_time_features=batch["future_time_features"]				,				)[0]

        a__	:  Union[str, Any]     =   torch.Size(
            (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)       )
        self.assertEqual(output.shape				,				lowerCamelCase__       )

        a__	:  List[str]     =   torch.tensor(
            [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]]				,				device=lowerCamelCase__       )
        self.assertTrue(torch.allclose(output[0, :3, :3]				,				lowerCamelCase__				,				atol=lowerCamelCase__       )       )

  def    _UpperCamelCase( self			:  Dict       ):
        a__	:  Optional[Any]     =   AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly"       ).to(lowerCamelCase__       )
        a__	:  int     =   prepare_batch("val-batch.pt"       )
        with torch.no_grad():
              a__	:  List[Any]     =   model(
                  past_values=batch["past_values"]				,				past_time_features=batch["past_time_features"]				,				past_observed_mask=batch["past_observed_mask"]				,				static_categorical_features=batch["static_categorical_features"]				,				).encoder_last_hidden_state
        a__	:  Any     =   torch.Size((64, model.config.context_length, model.config.d_model)       )
        self.assertEqual(output.shape				,				lowerCamelCase__       )

        a__	:  Tuple     =   torch.tensor(
            [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]]				,				device=lowerCamelCase__       )
        self.assertTrue(torch.allclose(output[0, :3, :3]				,				lowerCamelCase__				,				atol=lowerCamelCase__       )       )

  def    _UpperCamelCase( self			:  Dict       ):
        a__	:  Optional[Any]     =   AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly"       ).to(lowerCamelCase__       )
        a__	:  int     =   prepare_batch("val-batch.pt"       )
        with torch.no_grad():
              a__	:  List[str]     =   model.generate(
                  static_categorical_features=batch["static_categorical_features"]				,				past_time_features=batch["past_time_features"]				,				past_values=batch["past_values"]				,				future_time_features=batch["future_time_features"]				,				past_observed_mask=batch["past_observed_mask"]				,				)
        a__	:  List[str]     =   torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)       )
        self.assertEqual(outputs.sequences.shape				,				lowerCamelCase__       )

        a__	:  Tuple     =   torch.tensor([3130.6763, 4056.5293, 7053.0786]				,				device=lowerCamelCase__       )
        a__	:  Tuple     =   outputs.sequences.mean(dim=1       )
        self.assertTrue(torch.allclose(mean_prediction[0, -3:]				,				lowerCamelCase__				,				rtol=1E-1       )       )



import re





def      lowerCamelCase_					(							lowerCamelCase__       ):
				return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]"      ,					str_       )]





def      lowerCamelCase_					(							lowerCamelCase__       ):
				lowerCamelCase_         =     split_input(str_       )
				return "".join(
				    ["".join([char.capitalize() for char in sub_str]       ) for sub_str in string_split]       )





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__      ,					lowerCamelCase__       ):
				try:
								lowerCamelCase_         =     split_input(lowerCamelCase__       )
								if upper:
												lowerCamelCase_         =     "".join(
												    [
												        separator.join([char.upper() for char in sub_str]       )
												        for sub_str in string_split
												    ]       )
								else:
												lowerCamelCase_         =     "".join(
												    [
												        separator.join([char.lower() for char in sub_str]       )
												        for sub_str in string_split
												    ]       )
								return res_str
				except IndexError:
								return "not valid string"





def      lowerCamelCase_					(							lowerCamelCase__       ):
				return to_simple_case(lowerCamelCase__       )





def      lowerCamelCase_					(							lowerCamelCase__       ):
				try:
								lowerCamelCase_         =     to_simple_case(lowerCamelCase__       )
								return res_str[0].lower() + res_str[1:]
				except IndexError:
								return "not valid string"





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__       ):
				return to_complex_case(lowerCamelCase__      ,					lowerCamelCase__      ,					"_"       )





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__       ):
				return to_complex_case(lowerCamelCase__      ,					lowerCamelCase__      ,					"-"       )


if __name__ == "__main__":
			__import__('''doctest''').testmod()


