





'''simple docstring'''



import tempfile
import unittest

import numpy as np

from diffusers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionPipeline,
    PNDMScheduler,
)
from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
 import onnxruntime as ort


class    __SCREAMING_SNAKE_CASE						(    SCREAMING_SNAKE_CASE__       ,     unittest.TestCase      ):



     '''simple docstring'''






     UpperCAmelCase__							:    Dict     	= '''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'''

     def 						UpperCamelCase(       self    , lowerCamelCase=0						):
       _snake_case  		=					np.random.RandomState(lowerCamelCase						)
       _snake_case  		=					{
           "prompt": "A painting of a squirrel eating a burger",
           "generator": generator,
           "num_inference_steps": 2,
           "guidance_scale": 7.5,
           "output_type": "numpy",
       }
       return inputs

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					pipe(**lowerCamelCase						).images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _snake_case  		=					np.array([0.65072, 0.58492, 0.48219, 0.55521, 0.53180, 0.55939, 0.50697, 0.39800, 0.46455]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       _snake_case  		=					PNDMScheduler.from_config(pipe.scheduler.config    , skip_prk_steps=lowerCamelCase						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					pipe(**lowerCamelCase						).images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _snake_case  		=					np.array([0.65863, 0.59425, 0.49326, 0.56313, 0.53875, 0.56627, 0.51065, 0.39777, 0.46330]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       _snake_case  		=					LMSDiscreteScheduler.from_config(pipe.scheduler.config						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					pipe(**lowerCamelCase						).images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _snake_case  		=					np.array([0.53755, 0.60786, 0.47402, 0.49488, 0.51869, 0.49819, 0.47985, 0.38957, 0.44279]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       _snake_case  		=					EulerDiscreteScheduler.from_config(pipe.scheduler.config						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					pipe(**lowerCamelCase						).images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _snake_case  		=					np.array([0.53755, 0.60786, 0.47402, 0.49488, 0.51869, 0.49819, 0.47985, 0.38957, 0.44279]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       _snake_case  		=					EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					pipe(**lowerCamelCase						).images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _snake_case  		=					np.array([0.53817, 0.60812, 0.47384, 0.49530, 0.51894, 0.49814, 0.47984, 0.38958, 0.44271]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       _snake_case  		=					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					pipe(**lowerCamelCase						).images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _snake_case  		=					np.array([0.53895, 0.60808, 0.47933, 0.49608, 0.51886, 0.49950, 0.48053, 0.38957, 0.44200]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					3 * [inputs["prompt"]]

       # forward
       _snake_case  		=					pipe(**lowerCamelCase						)
       _snake_case  		=					output.images[0, -3:, -3:, -1]

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					3 * [inputs.pop("prompt"						)]

       _snake_case  		=					pipe.tokenizer(
           lowerCamelCase    , padding="max_length"    , max_length=pipe.tokenizer.model_max_length    , truncation=lowerCamelCase    , return_tensors="np"    , )
       _snake_case  		=					text_inputs["input_ids"]

       _snake_case  		=					pipe.text_encoder(input_ids=text_inputs.astype(np.intaa						)						)[0]

       _snake_case  		=					prompt_embeds

       # forward
       _snake_case  		=					pipe(**lowerCamelCase						)
       _snake_case  		=					output.images[0, -3:, -3:, -1]

       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()						).max() < 1e-4






     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="CPUExecutionProvider"						)
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					3 * ["this is a negative prompt"]
       _snake_case  		=					negative_prompt
       _snake_case  		=					3 * [inputs["prompt"]]

       # forward
       _snake_case  		=					pipe(**lowerCamelCase						)
       _snake_case  		=					output.images[0, -3:, -3:, -1]

       _snake_case  		=					self.get_dummy_inputs()
       _snake_case  		=					3 * [inputs.pop("prompt"						)]

       _snake_case  		=					[]
       for p in [prompt, negative_prompt]:
         _snake_case  		=					pipe.tokenizer(
             lowerCamelCase    , padding="max_length"    , max_length=pipe.tokenizer.model_max_length    , truncation=lowerCamelCase    , return_tensors="np"    , )
         _snake_case  		=					text_inputs["input_ids"]

         embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa						)						)[0]						)

       _snake_case		,		_snake_case  		=					embeds

       # forward
       _snake_case  		=					pipe(**lowerCamelCase						)
       _snake_case  		=					output.images[0, -3:, -3:, -1]

       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()						).max() < 1e-4







@nightly
@require_onnxruntime
@require_torch_gpu
class    __SCREAMING_SNAKE_CASE						(    unittest.TestCase      ):



     '''simple docstring'''






     @property
     def 						UpperCamelCase(       self						):
       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )

     @property
     def 						UpperCamelCase(       self						):
       _snake_case  		=					ort.SessionOptions()
       _snake_case  		=					False
       return options

     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(
           "CompVis/stable-diffusion-v1-4"    , revision="onnx"    , safety_checker=lowerCamelCase    , feature_extractor=lowerCamelCase    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
       sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					"A painting of a squirrel eating a burger"
       np.random.seed(0						)
       _snake_case  		=					sd_pipe([prompt]    , guidance_scale=6.0    , num_inference_steps=10    , output_type="np"						)
       _snake_case  		=					output.images

       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       _snake_case  		=					np.array([0.0452, 0.0390, 0.0087, 0.0350, 0.0617, 0.0364, 0.0544, 0.0523, 0.0720]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-3

     def 						UpperCamelCase(       self						):
       _snake_case  		=					DDIMScheduler.from_pretrained(
           "runwayml/stable-diffusion-v1-5"    , subfolder="scheduler"    , revision="onnx"						)
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5"    , revision="onnx"    , scheduler=lowerCamelCase    , safety_checker=lowerCamelCase    , feature_extractor=lowerCamelCase    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
       sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					"open neural network exchange"
       _snake_case  		=					np.random.RandomState(0						)
       _snake_case  		=					sd_pipe([prompt]    , guidance_scale=7.5    , num_inference_steps=10    , generator=lowerCamelCase    , output_type="np"						)
       _snake_case  		=					output.images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       _snake_case  		=					np.array([0.2867, 0.1974, 0.1481, 0.7294, 0.7251, 0.6667, 0.4194, 0.5642, 0.6486]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-3

     def 						UpperCamelCase(       self						):
       _snake_case  		=					LMSDiscreteScheduler.from_pretrained(
           "runwayml/stable-diffusion-v1-5"    , subfolder="scheduler"    , revision="onnx"						)
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5"    , revision="onnx"    , scheduler=lowerCamelCase    , safety_checker=lowerCamelCase    , feature_extractor=lowerCamelCase    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
       sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					"open neural network exchange"
       _snake_case  		=					np.random.RandomState(0						)
       _snake_case  		=					sd_pipe([prompt]    , guidance_scale=7.5    , num_inference_steps=10    , generator=lowerCamelCase    , output_type="np"						)
       _snake_case  		=					output.images
       _snake_case  		=					image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       _snake_case  		=					np.array([0.2306, 0.1959, 0.1593, 0.6549, 0.6394, 0.5408, 0.5065, 0.6010, 0.6161]						)

       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-3

     def 						UpperCamelCase(       self						):
       _snake_case  		=					0

       def test_callback_fn(lowerCamelCase    , lowerCamelCase    , lowerCamelCase						) -> None:
         _snake_case  		=					True
         nonlocal number_of_steps
         number_of_steps += 1
         if step == 0:
           assert latents.shape == (1, 4, 64, 64)
           _snake_case  		=					latents[0, -3:, -3:, -1]
           _snake_case  		=					np.array(
               [-0.6772, -0.3835, -1.2456, 0.1905, -1.0974, 0.6967, -1.9353, 0.0178, 1.0167]						)

           assert np.abs(latents_slice.flatten() - expected_slice						).max() < 1e-3
         elif step == 5:
           assert latents.shape == (1, 4, 64, 64)
           _snake_case  		=					latents[0, -3:, -3:, -1]
           _snake_case  		=					np.array(
               [-0.3351, 0.2241, -0.1837, -0.2325, -0.6577, 0.3393, -0.0241, 0.5899, 1.3875]						)

           assert np.abs(latents_slice.flatten() - expected_slice						).max() < 1e-3

       _snake_case  		=					False

       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5"    , revision="onnx"    , safety_checker=lowerCamelCase    , feature_extractor=lowerCamelCase    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
       pipe.set_progress_bar_config(disable=lowerCamelCase						)

       _snake_case  		=					"Andromeda galaxy in a bottle"

       _snake_case  		=					np.random.RandomState(0						)
       pipe(
           prompt=lowerCamelCase    , num_inference_steps=5    , guidance_scale=7.5    , generator=lowerCamelCase    , callback=lowerCamelCase    , callback_steps=1    , )
       assert test_callback_fn.has_been_called
       assert number_of_steps == 6






     def 						UpperCamelCase(       self						):
       _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5"    , revision="onnx"    , safety_checker=lowerCamelCase    , feature_extractor=lowerCamelCase    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
       assert isinstance(lowerCamelCase    , lowerCamelCase						)
       assert pipe.safety_checker is None

       _snake_case  		=					pipe("example prompt"    , num_inference_steps=2						).images[0]
       assert image is not None

       # check that there's no error when saving a pipeline with one of the models being None
       with tempfile.TemporaryDirectory() as tmpdirname:
         pipe.save_pretrained(lowerCamelCase						)
         _snake_case  		=					OnnxStableDiffusionPipeline.from_pretrained(lowerCamelCase						)

       # sanity check that the pipeline still works
       assert pipe.safety_checker is None
       _snake_case  		=					pipe("example prompt"    , num_inference_steps=2						).images[0]
       assert image is not None








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


SCREAMING_SNAKE_CASE__     :     Optional[Any]		    =       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_       :





	def __init__(							self      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE=16      ,     SCREAMING_SNAKE_CASE=13      ,     SCREAMING_SNAKE_CASE=7      ,     SCREAMING_SNAKE_CASE=14      ,     SCREAMING_SNAKE_CASE=10      ,     SCREAMING_SNAKE_CASE=19      ,     SCREAMING_SNAKE_CASE=5      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE=True      ,     SCREAMING_SNAKE_CASE=16      ,     SCREAMING_SNAKE_CASE=2      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE="gelu"      ,     SCREAMING_SNAKE_CASE=0.1      ,     SCREAMING_SNAKE_CASE=0.1      ,     SCREAMING_SNAKE_CASE=[1, 2, 3, 4, 5]      ,     SCREAMING_SNAKE_CASE=25      ,     SCREAMING_SNAKE_CASE=5      ,     )  ->					Union[str, Any]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						d_model
				SCREAMING_SNAKE_CASE_								=						parent
				SCREAMING_SNAKE_CASE_								=						batch_size
				SCREAMING_SNAKE_CASE_								=						prediction_length
				SCREAMING_SNAKE_CASE_								=						context_length
				SCREAMING_SNAKE_CASE_								=						cardinality
				SCREAMING_SNAKE_CASE_								=						num_time_features
				SCREAMING_SNAKE_CASE_								=						lags_sequence
				SCREAMING_SNAKE_CASE_								=						embedding_dimension
				SCREAMING_SNAKE_CASE_								=						is_training
				SCREAMING_SNAKE_CASE_								=						hidden_size
				SCREAMING_SNAKE_CASE_								=						num_hidden_layers
				SCREAMING_SNAKE_CASE_								=						num_attention_heads
				SCREAMING_SNAKE_CASE_								=						intermediate_size
				SCREAMING_SNAKE_CASE_								=						hidden_act
				SCREAMING_SNAKE_CASE_								=						hidden_dropout_prob
				SCREAMING_SNAKE_CASE_								=						attention_probs_dropout_prob

				SCREAMING_SNAKE_CASE_								=						context_length
				SCREAMING_SNAKE_CASE_								=						prediction_length + label_length
				SCREAMING_SNAKE_CASE_								=						label_length

				SCREAMING_SNAKE_CASE_								=						moving_average
				SCREAMING_SNAKE_CASE_								=						autocorrelation_factor





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




				"""simple docstring"""



				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 						A_(							self      ,     SCREAMING_SNAKE_CASE      )  ->					Optional[int]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						config.context_length + max(config.lags_sequence      )

				SCREAMING_SNAKE_CASE_								=						ids_tensor([self.batch_size, 1]      ,     config.cardinality[0]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, _past_length, config.num_time_features]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, _past_length]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, _past_length]      ) > 0.5

				# decoder inputs
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, config.prediction_length]      )

				SCREAMING_SNAKE_CASE_								=						{
				    '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 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						self.get_config()
				SCREAMING_SNAKE_CASE_								=						self.prepare_autoformer_inputs_dict(SCREAMING_SNAKE_CASE      )
				return config, inputs_dict





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.prepare_config_and_inputs()
				return config, inputs_dict





	def 						A_(							self      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerModel(config=SCREAMING_SNAKE_CASE      ).to(SCREAMING_SNAKE_CASE      ).eval()
				SCREAMING_SNAKE_CASE_								=						model(**SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						outputs.encoder_last_hidden_state
				SCREAMING_SNAKE_CASE_								=						outputs.last_hidden_state

				with tempfile.TemporaryDirectory() as tmpdirname:
							SCREAMING_SNAKE_CASE_								=						model.get_encoder()
							encoder.save_pretrained(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						AutoformerEncoder.from_pretrained(SCREAMING_SNAKE_CASE      ).to(SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						model.create_network_inputs(**SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]      )

				SCREAMING_SNAKE_CASE_								=						torch.cat(
				    (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])      ,     dim=-1      ,     )
				SCREAMING_SNAKE_CASE_								=						encoder(inputs_embeds=SCREAMING_SNAKE_CASE      )[0]
				self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3      )

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

				SCREAMING_SNAKE_CASE_								=						torch.cat(
				    (
				        torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)      ,     dim=1      ),
				        feature[:, config.context_length - config.label_length :, ...],
				    )      ,     dim=-1      ,     )
				SCREAMING_SNAKE_CASE_								=						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:
							SCREAMING_SNAKE_CASE_								=						model.get_decoder()
							decoder.save_pretrained(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						AutoformerDecoder.from_pretrained(SCREAMING_SNAKE_CASE      ).to(SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						decoder(
				    trend=SCREAMING_SNAKE_CASE      ,     inputs_embeds=SCREAMING_SNAKE_CASE      ,     encoder_hidden_states=SCREAMING_SNAKE_CASE      ,     )[0]

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




@require_torch
class       a_       (     SCREAMING_SNAKE_CASE__  ,    SCREAMING_SNAKE_CASE__  ,    unittest.TestCase     ):
	A    							= (AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
	A    							= (AutoformerForPrediction,) if is_torch_available() else ()
	A    							= {'''feature-extraction''': AutoformerModel} if is_torch_available() else {}
	A    							= False
	A    							= False
	A    							= False
	A    							= False
	A    							= False
	A    							= False





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerModelTester(self      )
				SCREAMING_SNAKE_CASE_								=						ConfigTester(self      ,     config_class=SCREAMING_SNAKE_CASE      ,     has_text_modality=SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					int:




				"""simple docstring"""



				self.config_tester.run_common_tests()





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs()
				for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )

							with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(SCREAMING_SNAKE_CASE      )
										SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						model_class.from_pretrained(SCREAMING_SNAKE_CASE      ,     output_loading_info=SCREAMING_SNAKE_CASE      )
							self.assertEqual(info['missing_keys']      ,     []      )





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_encoder_decoder_model_standalone(*SCREAMING_SNAKE_CASE      )





	@unittest.skip(reason='Model has no tokens embeddings'      )
	def 						A_(							self      )  ->					Union[str, Any]:




				"""simple docstring"""



				pass





	def 						A_(							self      )  ->					int:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						inspect.signature(getattr(SCREAMING_SNAKE_CASE      ,     'forward'      )      )
				# The main input is the name of the argument after `self`
				SCREAMING_SNAKE_CASE_								=						list(model_signature.parameters.keys()      )[1]
				self.assertEqual(AutoformerModel.main_input_name      ,     SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()

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

							SCREAMING_SNAKE_CASE_								=						[
							    '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(SCREAMING_SNAKE_CASE      )]      ,     SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					str:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()
				SCREAMING_SNAKE_CASE_								=						True

				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'seq_length'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'decoder_seq_length'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'encoder_seq_length'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'd_model'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'num_attention_heads'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						d_model // num_attention_heads

				for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						False
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							model.to(SCREAMING_SNAKE_CASE      )
							model.eval()
							with torch.no_grad():
										SCREAMING_SNAKE_CASE_								=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )      )
							SCREAMING_SNAKE_CASE_								=						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
							self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     self.model_tester.num_hidden_layers      )

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

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

							SCREAMING_SNAKE_CASE_								=						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(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )

							# decoder attentions
							SCREAMING_SNAKE_CASE_								=						outputs.decoder_attentions
							self.assertIsInstance(SCREAMING_SNAKE_CASE      ,     (list, tuple)      )
							self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     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
							SCREAMING_SNAKE_CASE_								=						outputs.cross_attentions
							self.assertIsInstance(SCREAMING_SNAKE_CASE      ,     (list, tuple)      )
							self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     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
				SCREAMING_SNAKE_CASE_								=						True
				SCREAMING_SNAKE_CASE_								=						True
				SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
				model.to(SCREAMING_SNAKE_CASE      )
				model.eval()
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )      )

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

				SCREAMING_SNAKE_CASE_								=						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

				self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     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 						A_(							self      )  ->					Any:




				"""simple docstring"""



				super().test_retain_grad_hidden_states_attentions()




def lowercase    (  SCREAMING_SNAKE_CASE="train-batch.pt" )			->							str:


			'''simple docstring'''







			SCREAMING_SNAKE_CASE_								=						hf_hub_download(repo_id='hf-internal-testing/tourism-monthly-batch'       ,    filename=SCREAMING_SNAKE_CASE       ,    repo_type='dataset' )
			SCREAMING_SNAKE_CASE_								=						torch.load(SCREAMING_SNAKE_CASE       ,    map_location=SCREAMING_SNAKE_CASE )
			return batch



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





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly'      ).to(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						prepare_batch()

				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						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]

				SCREAMING_SNAKE_CASE_								=						torch.Size(
				    (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)      )
				self.assertEqual(output.shape      ,     SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						torch.tensor(
				    [[0.3_5_9_3, -1.3_3_9_8, 0.6_3_3_0], [0.2_2_7_9, 1.5_3_9_6, -0.1_7_9_2], [0.0_4_5_0, 1.3_2_2_5, -0.2_3_3_5]]      ,     device=SCREAMING_SNAKE_CASE      )
				self.assertTrue(torch.allclose(output[0, :3, :3]      ,     SCREAMING_SNAKE_CASE      ,     atol=SCREAMING_SNAKE_CASE      )      )





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'      ).to(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						prepare_batch('val-batch.pt'      )
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						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
				SCREAMING_SNAKE_CASE_								=						torch.Size((64, model.config.context_length, model.config.d_model)      )
				self.assertEqual(output.shape      ,     SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						torch.tensor(
				    [[-0.0_7_3_4, -0.9_0_3_6, 0.8_3_5_8], [4.7_1_8_6, 2.4_1_1_3, 1.9_5_8_1], [1.7_9_5_3, 2.3_5_5_8, 1.2_9_7_0]]      ,     device=SCREAMING_SNAKE_CASE      )
				self.assertTrue(torch.allclose(output[0, :3, :3]      ,     SCREAMING_SNAKE_CASE      ,     atol=SCREAMING_SNAKE_CASE      )      )





	def 						A_(							self      )  ->					Any:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'      ).to(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						prepare_batch('val-batch.pt'      )
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						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']      ,     )
				SCREAMING_SNAKE_CASE_								=						torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)      )
				self.assertEqual(outputs.sequences.shape      ,     SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						torch.tensor([3_1_3_0.6_7_6_3, 4_0_5_6.5_2_9_3, 7_0_5_3.0_7_8_6]      ,     device=SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						outputs.sequences.mean(dim=1      )
				self.assertTrue(torch.allclose(mean_prediction[0, -3:]      ,     SCREAMING_SNAKE_CASE      ,     rtol=1e-1      )      )
