





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

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


_snake_case           =			logging.get_logger(__name__)

_snake_case           =			{
    """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json""",
    # See all Marian models at https://huggingface.co/models?filter=marian
}





class  lowerCAmelCase							(       lowercase_ ):
  __lowerCamelCase		=      'marian'
  __lowerCamelCase		=      ['past_key_values']
  __lowerCamelCase		=      {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







  def __init__(      self  :int   ,							_lowercase  :Dict=5_81_01   ,							_lowercase  :int=None   ,							_lowercase  :str=10_24   ,							_lowercase  :Optional[int]=12   ,							_lowercase  :int=40_96   ,							_lowercase  :List[Any]=16   ,							_lowercase  :int=12   ,							_lowercase  :List[Any]=40_96   ,							_lowercase  :List[str]=16   ,							_lowercase  :int=0.0   ,							_lowercase  :str=0.0   ,							_lowercase  :List[Any]=True   ,							_lowercase  :str=True   ,							_lowercase  :Optional[int]="gelu"   ,							_lowercase  :Tuple=10_24   ,							_lowercase  :Union[str, Any]=0.1   ,							_lowercase  :Optional[Any]=0.0   ,							_lowercase  :Dict=0.0   ,							_lowercase  :str=0.02   ,							_lowercase  :Optional[Any]=5_81_00   ,							_lowercase  :Union[str, Any]=False   ,							_lowercase  :Union[str, Any]=5_81_00   ,							_lowercase  :Union[str, Any]=0   ,							_lowercase  :Any=0   ,							_lowercase  :Optional[int]=True   ,							**_lowercase  :int   ,							):






     '''simple docstring'''





     lowercase__			       =  vocab_size
     lowercase__			       =  decoder_vocab_size or vocab_size
     lowercase__			       =  max_position_embeddings
     lowercase__			       =  d_model
     lowercase__			       =  encoder_ffn_dim
     lowercase__			       =  encoder_layers
     lowercase__			       =  encoder_attention_heads
     lowercase__			       =  decoder_ffn_dim
     lowercase__			       =  decoder_layers
     lowercase__			       =  decoder_attention_heads
     lowercase__			       =  dropout
     lowercase__			       =  attention_dropout
     lowercase__			       =  activation_dropout
     lowercase__			       =  activation_function
     lowercase__			       =  init_std
     lowercase__			       =  encoder_layerdrop
     lowercase__			       =  decoder_layerdrop
     lowercase__			       =  use_cache
     lowercase__			       =  encoder_layers
     lowercase__			       =  scale_embedding  # scale factor will be sqrt(d_model) if True
     lowercase__			       =  share_encoder_decoder_embeddings
     super().__init__(
         pad_token_id=_lowercase   ,							eos_token_id=_lowercase   ,							is_encoder_decoder=_lowercase   ,							decoder_start_token_id=_lowercase   ,							forced_eos_token_id=_lowercase   ,							**_lowercase   ,							)





class  lowerCAmelCase							(       lowercase_ ):







  @property
  # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
            ] )

        if self.use_past:
           lowercase__			       =  {0: "batch"}
           lowercase__			       =  {0: "batch", 1: "past_decoder_sequence + sequence"}
        else:
           lowercase__			       =  {0: "batch", 1: "decoder_sequence"}
           lowercase__			       =  {0: "batch", 1: "decoder_sequence"}

        if self.use_past:
           self.fill_with_past_key_values_(_lowercase   ,							direction="inputs" )
     elif self.task == "causal-lm":
        # TODO: figure this case out.
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
            ] )
        if self.use_past:
           lowercase__       ,							lowercase__			       =  self.num_layers
           for i in range(_lowercase ):
              lowercase__			       =  {0: "batch", 2: "past_sequence + sequence"}
              lowercase__			       =  {0: "batch", 2: "past_sequence + sequence"}
     else:
        lowercase__			       =  OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
                ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
                ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
            ] )

     return common_inputs







  @property
  # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  super().outputs
     else:
        lowercase__			       =  super(_lowercase   ,							self ).outputs
        if self.use_past:
           lowercase__       ,							lowercase__			       =  self.num_layers
           for i in range(_lowercase ):
              lowercase__			       =  {0: "batch", 2: "past_sequence + sequence"}
              lowercase__			       =  {0: "batch", 2: "past_sequence + sequence"}
     return common_outputs







  def   UpperCAmelCase					(      self  :Optional[int]   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )

     # Generate decoder inputs
     lowercase__			       =  seq_length if not self.use_past else 1
     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )
     lowercase__			       =  {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
     lowercase__			       =  dict(**_lowercase   ,							**_lowercase )

     if self.use_past:
        if not is_torch_available():
           raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." )
        else:
           import torch
        lowercase__       ,							lowercase__			       =  common_inputs["input_ids"].shape
        lowercase__			       =  common_inputs["decoder_input_ids"].shape[1]
        lowercase__       ,							lowercase__			       =  self.num_attention_heads
        lowercase__			       =  (
            batch,
            num_encoder_attention_heads,
            encoder_seq_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )
        lowercase__			       =  decoder_seq_length + 3
        lowercase__			       =  (
            batch,
            num_decoder_attention_heads,
            decoder_past_length,
            self._config.hidden_size // num_decoder_attention_heads,
        )

        lowercase__			       =  torch.cat(
            [common_inputs["decoder_attention_mask"], torch.ones(_lowercase   ,							_lowercase )]   ,							dim=1 )

        lowercase__			       =  []
        # If the number of encoder and decoder layers are present in the model configuration, both are considered
        lowercase__       ,							lowercase__			       =  self.num_layers
        lowercase__			       =  min(_lowercase   ,							_lowercase )
        lowercase__			       =  max(_lowercase   ,							_lowercase ) - min_num_layers
        lowercase__			       =  "encoder" if num_encoder_layers > num_decoder_layers else "decoder"

        for _ in range(_lowercase ):
           common_inputs["past_key_values"].append(
               (
                   torch.zeros(_lowercase ),
                   torch.zeros(_lowercase ),
                   torch.zeros(_lowercase ),
                   torch.zeros(_lowercase ),
               ) )
        # TODO: test this.
        lowercase__			       =  encoder_shape if remaining_side_name == "encoder" else decoder_shape
        for _ in range(_lowercase   ,							_lowercase ):
           common_inputs["past_key_values"].append((torch.zeros(_lowercase ), torch.zeros(_lowercase )) )
     return common_inputs







  def   UpperCAmelCase					(      self  :Tuple   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  self._generate_dummy_inputs_for_encoder_and_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )

     if self.use_past:
        if not is_torch_available():
           raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." )
        else:
           import torch
        lowercase__       ,							lowercase__			       =  common_inputs["input_ids"].shape
        # Not using the same length for past_key_values
        lowercase__			       =  seqlen + 2
        lowercase__       ,							lowercase__			       =  self.num_layers
        lowercase__       ,							lowercase__			       =  self.num_attention_heads
        lowercase__			       =  (
            batch,
            num_encoder_attention_heads,
            past_key_values_length,
            self._config.hidden_size // num_encoder_attention_heads,
        )

        lowercase__			       =  common_inputs["attention_mask"].dtype
        lowercase__			       =  torch.cat(
            [common_inputs["attention_mask"], torch.ones(_lowercase   ,							_lowercase   ,							dtype=_lowercase )]   ,							dim=1 )
        lowercase__			       =  [
            (torch.zeros(_lowercase ), torch.zeros(_lowercase )) for _ in range(_lowercase )
        ]
     return common_inputs







  def   UpperCAmelCase					(      self  :Optional[int]   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     lowercase__			       =  compute_effective_axis_dimension(
         _lowercase   ,							fixed_dimension=OnnxConfig.default_fixed_batch   ,							num_token_to_add=0 )

     # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
     lowercase__			       =  tokenizer.num_special_tokens_to_add(_lowercase )
     lowercase__			       =  compute_effective_axis_dimension(
         _lowercase   ,							fixed_dimension=OnnxConfig.default_fixed_sequence   ,							num_token_to_add=_lowercase )

     # Generate dummy inputs according to compute batch and sequence
     lowercase__			       =  [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size
     lowercase__			       =  dict(tokenizer(_lowercase   ,							return_tensors=_lowercase ) )
     return common_inputs







  def   UpperCAmelCase					(      self  :Dict   ,							_lowercase  :PreTrainedTokenizer   ,							_lowercase  :int = -1   ,							_lowercase  :int = -1   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[TensorType] = None   ,							):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  self._generate_dummy_inputs_for_default_and_seqaseq_lm(
            _lowercase   ,							batch_size=_lowercase   ,							seq_length=_lowercase   ,							is_pair=_lowercase   ,							framework=_lowercase )

     else:
        lowercase__			       =  self._generate_dummy_inputs_for_causal_lm(
            _lowercase   ,							batch_size=_lowercase   ,							seq_length=_lowercase   ,							is_pair=_lowercase   ,							framework=_lowercase )

     return common_inputs







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :Dict   ,							_lowercase  :str   ,							_lowercase  :Tuple ):






     '''simple docstring'''





     if self.task in ["default", "seq2seq-lm"]:
        lowercase__			       =  super()._flatten_past_key_values_(_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )
     else:
        lowercase__			       =  super(_lowercase   ,							self )._flatten_past_key_values_(
            _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )







  @property
  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     return 1e-4













import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


def _A       (   __magic_name__	):
   lowercase__			       =  [tensor.shape for tensor in tensor_list]
   return all(shape == shapes[0] for shape in shapes[1:]	)





class  lowerCAmelCase							(       lowercase_							,							lowercase_							,							lowercase_							,							unittest.TestCase ):
  __lowerCamelCase		=      StableDiffusionLatentUpscalePipeline
  __lowerCamelCase		=      TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
      'height',
      'width',
      'cross_attention_kwargs',
      'negative_prompt_embeds',
      'prompt_embeds',
  }
  __lowerCamelCase		=      PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'}
  __lowerCamelCase		=      TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
  __lowerCamelCase		=      frozenset(
      [] )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
  __lowerCamelCase		=      frozenset([] )

  __lowerCamelCase		=      True







  @property
  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     lowercase__			       =  1
     lowercase__			       =  4
     lowercase__			       =  (16, 16)

     lowercase__			       =  floats_tensor((batch_size, num_channels) + sizes   ,							rng=random.Random(0 ) ).to(_lowercase )
     return image







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     torch.manual_seed(0 )
     lowercase__			       =  UNetaDConditionModel(
         act_fn="gelu"   ,							attention_head_dim=8   ,							norm_num_groups=_lowercase   ,							block_out_channels=[32, 32, 64, 64]   ,							time_cond_proj_dim=1_60   ,							conv_in_kernel=1   ,							conv_out_kernel=1   ,							cross_attention_dim=32   ,							down_block_types=(
             "KDownBlock2D",
             "KCrossAttnDownBlock2D",
             "KCrossAttnDownBlock2D",
             "KCrossAttnDownBlock2D",
         )   ,							in_channels=8   ,							mid_block_type=_lowercase   ,							only_cross_attention=_lowercase   ,							out_channels=5   ,							resnet_time_scale_shift="scale_shift"   ,							time_embedding_type="fourier"   ,							timestep_post_act="gelu"   ,							up_block_types=("KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KUpBlock2D")   ,							)
     lowercase__			       =  AutoencoderKL(
         block_out_channels=[32, 32, 64, 64]   ,							in_channels=3   ,							out_channels=3   ,							down_block_types=[
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
         ]   ,							up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"]   ,							latent_channels=4   ,							)
     lowercase__			       =  EulerDiscreteScheduler(prediction_type="sample" )
     lowercase__			       =  CLIPTextConfig(
         bos_token_id=0   ,							eos_token_id=2   ,							hidden_size=32   ,							intermediate_size=37   ,							layer_norm_eps=1e-05   ,							num_attention_heads=4   ,							num_hidden_layers=5   ,							pad_token_id=1   ,							vocab_size=10_00   ,							hidden_act="quick_gelu"   ,							projection_dim=5_12   ,							)
     lowercase__			       =  CLIPTextModel(_lowercase )
     lowercase__			       =  CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" )

     lowercase__			       =  {
         "unet": model.eval(),
         "vae": vae.eval(),
         "scheduler": scheduler,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
     }

     return components







  def   UpperCAmelCase					(      self  :Dict   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :int=0 ):






     '''simple docstring'''





     if str(_lowercase ).startswith("mps" ):
        lowercase__			       =  torch.manual_seed(_lowercase )
     else:
        lowercase__			       =  torch.Generator(device=_lowercase ).manual_seed(_lowercase )
     lowercase__			       =  {
         "prompt": "A painting of a squirrel eating a burger",
         "image": self.dummy_image.cpu(),
         "generator": generator,
         "num_inference_steps": 2,
         "output_type": "numpy",
     }
     return inputs







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  "cpu"

     lowercase__			       =  self.get_dummy_components()
     lowercase__			       =  self.pipeline_class(**_lowercase )
     pipe.to(_lowercase )
     pipe.set_progress_bar_config(disable=_lowercase )

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

     self.assertEqual(image.shape   ,							(1, 2_56, 2_56, 3) )
     lowercase__			       =  np.array(
         [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055] )
     lowercase__			       =  np.abs(image_slice.flatten() - expected_slice ).max()
     self.assertLessEqual(_lowercase   ,							1e-3 )







  def   UpperCAmelCase					(      self  :Any ):






     '''simple docstring'''





     super().test_attention_slicing_forward_pass(expected_max_diff=7e-3 )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     super().test_cpu_offload_forward_pass(expected_max_diff=3e-3 )







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     super().test_inference_batch_single_identical(expected_max_diff=7e-3 )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3 )







  def   UpperCAmelCase					(      self  :Optional[int] ):






     '''simple docstring'''





     super().test_save_load_local(expected_max_difference=3e-3 )







  def   UpperCAmelCase					(      self  :Tuple ):






     '''simple docstring'''





     super().test_save_load_optional_components(expected_max_difference=3e-3 )







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     lowercase__			       =  [
         "DDIMScheduler",
         "DDPMScheduler",
         "PNDMScheduler",
         "HeunDiscreteScheduler",
         "EulerAncestralDiscreteScheduler",
         "KDPM2DiscreteScheduler",
         "KDPM2AncestralDiscreteScheduler",
         "DPMSolverSDEScheduler",
     ]
     lowercase__			       =  self.get_dummy_components()
     lowercase__			       =  self.pipeline_class(**_lowercase )

     # make sure that PNDM does not need warm-up
     pipe.scheduler.register_to_config(skip_prk_steps=_lowercase )

     pipe.to(_lowercase )
     pipe.set_progress_bar_config(disable=_lowercase )
     lowercase__			       =  self.get_dummy_inputs(_lowercase )
     lowercase__			       =  2

     lowercase__			       =  []
     for scheduler_enum in KarrasDiffusionSchedulers:
        if scheduler_enum.name in skip_schedulers:
           # no sigma schedulers are not supported
           # no schedulers
           continue

        lowercase__			       =  getattr(_lowercase   ,							scheduler_enum.name )
        lowercase__			       =  scheduler_cls.from_config(pipe.scheduler.config )
        lowercase__			       =  pipe(**_lowercase )[0]
        outputs.append(_lowercase )

     assert check_same_shape(_lowercase )





@require_torch_gpu
@slow
class  lowerCAmelCase							(       unittest.TestCase ):







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







  def   UpperCAmelCase					(      self  :str ):






     '''simple docstring'''





     lowercase__			       =  torch.manual_seed(33 )

     lowercase__			       =  StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"   ,							torch_dtype=torch.floataa )
     pipe.to("cuda" )

     lowercase__			       =  StableDiffusionLatentUpscalePipeline.from_pretrained(
         "stabilityai/sd-x2-latent-upscaler"   ,							torch_dtype=torch.floataa )
     upscaler.to("cuda" )

     lowercase__			       =  "a photo of an astronaut high resolution, unreal engine, ultra realistic"

     lowercase__			       =  pipe(_lowercase   ,							generator=_lowercase   ,							output_type="latent" ).images

     lowercase__			       =  upscaler(
         prompt=_lowercase   ,							image=_lowercase   ,							num_inference_steps=20   ,							guidance_scale=0   ,							generator=_lowercase   ,							output_type="np"   ,							).images[0]

     lowercase__			       =  load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy" )
     assert np.abs((expected_image - image).mean() ) < 5e-2







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     lowercase__			       =  torch.manual_seed(33 )

     lowercase__			       =  StableDiffusionLatentUpscalePipeline.from_pretrained(
         "stabilityai/sd-x2-latent-upscaler"   ,							torch_dtype=torch.floataa )
     upscaler.to("cuda" )

     lowercase__			       =  "the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"

     lowercase__			       =  load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png" )

     lowercase__			       =  upscaler(
         prompt=_lowercase   ,							image=_lowercase   ,							num_inference_steps=20   ,							guidance_scale=0   ,							generator=_lowercase   ,							output_type="np"   ,							).images[0]

     lowercase__			       =  load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy" )
     assert np.abs((expected_image - image).max() ) < 5e-2






