






'''simple docstring'''







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


UpperCamelCase     :   Optional[Any]	      =		logging.get_logger(__name__)

UpperCamelCase     :   List[str]	      =		{
    '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 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		_UpperCamelCase						=       'marian'
		_UpperCamelCase						=       ['past_key_values']
		_UpperCamelCase						=       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}






		def __init__(   self ,_lowerCAmelCase=5_81_01 ,_lowerCAmelCase=None ,_lowerCAmelCase=10_24 ,_lowerCAmelCase=12 ,_lowerCAmelCase=40_96 ,_lowerCAmelCase=16 ,_lowerCAmelCase=12 ,_lowerCAmelCase=40_96 ,_lowerCAmelCase=16 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=True ,_lowerCAmelCase=True ,_lowerCAmelCase="gelu" ,_lowerCAmelCase=10_24 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.02 ,_lowerCAmelCase=5_81_00 ,_lowerCAmelCase=False ,_lowerCAmelCase=5_81_00 ,_lowerCAmelCase=0 ,_lowerCAmelCase=0 ,_lowerCAmelCase=True ,**_lowerCAmelCase ,):
								lowerCamelCase__								=	vocab_size
								lowerCamelCase__								=	decoder_vocab_size or vocab_size
								lowerCamelCase__								=	max_position_embeddings
								lowerCamelCase__								=	d_model
								lowerCamelCase__								=	encoder_ffn_dim
								lowerCamelCase__								=	encoder_layers
								lowerCamelCase__								=	encoder_attention_heads
								lowerCamelCase__								=	decoder_ffn_dim
								lowerCamelCase__								=	decoder_layers
								lowerCamelCase__								=	decoder_attention_heads
								lowerCamelCase__								=	dropout
								lowerCamelCase__								=	attention_dropout
								lowerCamelCase__								=	activation_dropout
								lowerCamelCase__								=	activation_function
								lowerCamelCase__								=	init_std
								lowerCamelCase__								=	encoder_layerdrop
								lowerCamelCase__								=	decoder_layerdrop
								lowerCamelCase__								=	use_cache
								lowerCamelCase__								=	encoder_layers
								lowerCamelCase__								=	scale_embedding  # scale factor will be sqrt(d_model) if True
								lowerCamelCase__								=	share_encoder_decoder_embeddings
								super().__init__(
								    pad_token_id=_lowerCAmelCase ,eos_token_id=_lowerCAmelCase ,is_encoder_decoder=_lowerCAmelCase ,decoder_start_token_id=_lowerCAmelCase ,forced_eos_token_id=_lowerCAmelCase ,**_lowerCAmelCase ,)




class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		@property
		# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
		def       UpperCamelCase_       (   self							):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	OrderedDict(
														    [
														        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
														        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
														    ]							)

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

														if self.use_past:
																				self.fill_with_past_key_values_(_lowerCAmelCase ,direction="""inputs"""							)
								elif self.task == "causal-lm":
														# TODO: figure this case out.
														lowerCamelCase__								=	OrderedDict(
														    [
														        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
														        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
														    ]							)
														if self.use_past:
																				lowerCamelCase__							,      lowerCamelCase__								=	self.num_layers
																				for i in range(_lowerCAmelCase							):
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
								else:
														lowerCamelCase__								=	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							):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	super().outputs
								else:
														lowerCamelCase__								=	super(_lowerCAmelCase ,self							).outputs
														if self.use_past:
																				lowerCamelCase__							,      lowerCamelCase__								=	self.num_layers
																				for i in range(_lowerCAmelCase							):
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
								return common_outputs






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								lowerCamelCase__								=	self._generate_dummy_inputs_for_encoder_and_decoder(
								    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

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

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

														lowerCamelCase__								=	torch.cat(
														    [common_inputs["""decoder_attention_mask"""], torch.ones(_lowerCAmelCase ,_lowerCAmelCase							)] ,dim=1							)

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

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






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								lowerCamelCase__								=	self._generate_dummy_inputs_for_encoder_and_decoder(
								    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

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

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






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								# Copied from OnnxConfig.generate_dummy_inputs
								# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
								# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
								lowerCamelCase__								=	compute_effective_axis_dimension(
								    _lowerCAmelCase ,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
								lowerCamelCase__								=	tokenizer.num_special_tokens_to_add(_lowerCAmelCase							)
								lowerCamelCase__								=	compute_effective_axis_dimension(
								    _lowerCAmelCase ,fixed_dimension=OnnxConfig.default_fixed_sequence ,num_token_to_add=_lowerCAmelCase							)

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






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	self._generate_dummy_inputs_for_default_and_seqaseq_lm(
														    _lowerCAmelCase ,batch_size=_lowerCAmelCase ,seq_length=_lowerCAmelCase ,is_pair=_lowerCAmelCase ,framework=_lowerCAmelCase							)

								else:
														lowerCamelCase__								=	self._generate_dummy_inputs_for_causal_lm(
														    _lowerCAmelCase ,batch_size=_lowerCAmelCase ,seq_length=_lowerCAmelCase ,is_pair=_lowerCAmelCase ,framework=_lowerCAmelCase							)

								return common_inputs






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	super()._flatten_past_key_values_(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)
								else:
														lowerCamelCase__								=	super(_lowerCAmelCase ,self							)._flatten_past_key_values_(
														    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)






		@property
		def       UpperCamelCase_       (   self							):
								return 1E-4






import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


a_      :      Optional[int]    =     False
@skip_mps
class    __UpperCamelCase			(       _lowercase	,	_lowercase	,	_lowercase	,	unittest.TestCase				):

 """simple docstring"""



 _lowercase   :    List[Any]      						=					StableDiffusionAttendAndExcitePipeline
 _lowercase   :    str      						=					False
 _lowercase   :    List[str]      						=					TEXT_TO_IMAGE_PARAMS
 _lowercase   :    Optional[int]      						=					TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''}				)
 _lowercase   :    Union[str, Any]      						=					TEXT_TO_IMAGE_IMAGE_PARAMS
 _lowercase   :    List[Any]      						=					TEXT_TO_IMAGE_IMAGE_PARAMS

 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			Dict:
  super().setUpClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			Tuple:
  super().tearDownClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  torch.manual_seed(0      )
  a__												= UNetaDConditionModel(
      block_out_channels=(3_2, 6_4)		,				layers_per_block=1		,				sample_size=3_2		,				in_channels=4		,				out_channels=4		,				down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')		,				up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')		,				cross_attention_dim=3_2		,				attention_head_dim=(2, 4)		,				use_linear_projection=SCREAMING_SNAKE_CASE		,				)
  a__												= DDIMScheduler(
      beta_start=0.0_00_85		,				beta_end=0.0_12		,				beta_schedule='''scaled_linear'''		,				clip_sample=SCREAMING_SNAKE_CASE		,				set_alpha_to_one=SCREAMING_SNAKE_CASE		,				)
  torch.manual_seed(0      )
  a__												= AutoencoderKL(
      block_out_channels=[3_2, 6_4]		,				in_channels=3		,				out_channels=3		,				down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']		,				up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']		,				latent_channels=4		,				sample_size=1_2_8		,				)
  torch.manual_seed(0      )
  a__												= CLIPTextConfig(
      bos_token_id=0		,				eos_token_id=2		,				hidden_size=3_2		,				intermediate_size=3_7		,				layer_norm_eps=1e-0_5		,				num_attention_heads=4		,				num_hidden_layers=5		,				pad_token_id=1		,				vocab_size=1_0_0_0		,				hidden_act='''gelu'''		,				projection_dim=5_1_2		,				)
  a__												= CLIPTextModel(SCREAMING_SNAKE_CASE      )
  a__												= CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

  a__												= {
      '''unet''': unet,
      '''scheduler''': scheduler,
      '''vae''': vae,
      '''text_encoder''': text_encoder,
      '''tokenizer''': tokenizer,
      '''safety_checker''': None,
      '''feature_extractor''': None,
  }

  return components

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=0      ) ->			Dict:
  if str(SCREAMING_SNAKE_CASE      ).startswith('''mps'''      ):
   a__												= torch.manual_seed(SCREAMING_SNAKE_CASE      )
  else:
   a__												= torch.Generator(device=SCREAMING_SNAKE_CASE      ).manual_seed(SCREAMING_SNAKE_CASE      )
  a__												= a__												= {
      '''prompt''': '''a cat and a frog''',
      '''token_indices''': [2, 5],
      '''generator''': generator,
      '''num_inference_steps''': 1,
      '''guidance_scale''': 6.0,
      '''output_type''': '''numpy''',
      '''max_iter_to_alter''': 2,
      '''thresholds''': {0: 0.7},
  }
  return inputs

 def 						_UpperCAmelCase       (	self      ) ->			str:
  a__												= '''cpu'''

  a__												= self.get_dummy_components()
  a__												= self.pipeline_class(**SCREAMING_SNAKE_CASE      )
  pipe.to(SCREAMING_SNAKE_CASE      )
  pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE      )

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

  self.assertEqual(image.shape		,				(1, 6_4, 6_4, 3)      )
  a__												= np.array(
      [0.63_90_53_64, 0.62_89_73_07, 0.48_59_90_17, 0.5_13_36_24, 0.5_55_00_48, 0.45_76_95_16, 0.50_32_69_73, 0.5_02_31_39, 0.45_38_44_96]      )
  a__												= np.abs(image_slice.flatten() - expected_slice      ).max()
  self.assertLessEqual(SCREAMING_SNAKE_CASE		,				1e-3      )

 def 						_UpperCAmelCase       (	self      ) ->			str:
  super().test_cpu_offload_forward_pass(expected_max_diff=5e-4      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
  self._test_inference_batch_consistent(batch_sizes=[1, 2]      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  self._test_inference_batch_single_identical(batch_size=2		,				expected_max_diff=7e-4      )

 def 						_UpperCAmelCase       (	self      ) ->			int:
  super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3      )

 def 						_UpperCAmelCase       (	self      ) ->			Optional[Any]:
  super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4      )

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  super().test_save_load_local(expected_max_difference=5e-4      )



 def 						_UpperCAmelCase       (	self      ) ->			str:
  super().test_save_load_optional_components(expected_max_difference=4e-4      )





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

 """simple docstring"""



 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			Optional[int]:
  super().setUpClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			str:
  super().tearDownClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  super().tearDown()
  gc.collect()
  torch.cuda.empty_cache()



 def 						_UpperCAmelCase       (	self      ) ->			str:
  a__												= torch.manual_seed(5_1      )

  a__												= StableDiffusionAttendAndExcitePipeline.from_pretrained(
      '''CompVis/stable-diffusion-v1-4'''		,				safety_checker=SCREAMING_SNAKE_CASE		,				torch_dtype=torch.floataa      )
  pipe.to('''cuda'''      )

  a__												= '''a painting of an elephant with glasses'''
  a__												= [5, 7]

  a__												= pipe(
      prompt=SCREAMING_SNAKE_CASE		,				token_indices=SCREAMING_SNAKE_CASE		,				guidance_scale=7.5		,				generator=SCREAMING_SNAKE_CASE		,				num_inference_steps=5		,				max_iter_to_alter=5		,				output_type='''numpy'''		,				).images[0]

  a__												= load_numpy(
      '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy'''      )
  assert np.abs((expected_image - image).max()      ) < 5e-1





