




import gc
import os
import tempfile
import unittest

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNetaDConditionModel
from diffusers.loaders import AttnProcsLayers, LoraLoaderMixin
from diffusers.models.attention_processor import (
    Attention,
    AttnProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRAAttnProcessor2_0,
    LoRAXFormersAttnProcessor,
    XFormersAttnProcessor,
)
from diffusers.utils import TEXT_ENCODER_ATTN_MODULE, floats_tensor, torch_device







def 			lowerCAmelCase  (     UpperCAmelCase	)  ->Dict:






					"""simple docstring"""






					__magic_name__ :							Optional[int]		       =							{}
					for name in unet.attn_processors.keys():
										__magic_name__ :							Dict		       =							None if name.endswith('''attn1.processor'''	) else unet.config.cross_attention_dim
										if name.startswith('''mid_block'''	):
															__magic_name__ :							List[Any]		       =							unet.config.block_out_channels[-1]
										elif name.startswith('''up_blocks'''	):
															__magic_name__ :							Tuple		       =							int(name[len('''up_blocks.'''	)]	)
															__magic_name__ :							Tuple		       =							list(reversed(unet.config.block_out_channels	)	)[block_id]
										elif name.startswith('''down_blocks'''	):
															__magic_name__ :							str		       =							int(name[len('''down_blocks.'''	)]	)
															__magic_name__ :							List[Any]		       =							unet.config.block_out_channels[block_id]
										__magic_name__ :							Optional[int]		       =							(
										    LoRAAttnProcessor2_0 if hasattr(UpperCAmelCase,     '''scaled_dot_product_attention'''	) else LoRAAttnProcessor
										)
										__magic_name__ :							Optional[Any]		       =							lora_attn_processor_class(
										    hidden_size=UpperCAmelCase,     cross_attention_dim=UpperCAmelCase	)
					__magic_name__ :							Tuple		       =							AttnProcsLayers(UpperCAmelCase	)
					return lora_attn_procs, unet_lora_layers







def 			lowerCAmelCase  (     UpperCAmelCase	)  ->str:






					"""simple docstring"""






					__magic_name__ :							List[str]		       =							{}
					__magic_name__ :							List[str]		       =							(
					    LoRAAttnProcessor2_0 if hasattr(UpperCAmelCase,     '''scaled_dot_product_attention'''	) else LoRAAttnProcessor
					)
					for name, module in text_encoder.named_modules():
										if name.endswith(UpperCAmelCase	):
															__magic_name__ :							Optional[int]		       =							lora_attn_processor_class(
															    hidden_size=module.out_proj.out_features,     cross_attention_dim=UpperCAmelCase	)
					return text_lora_attn_procs







def 			lowerCAmelCase  (     UpperCAmelCase	)  ->Optional[int]:






					"""simple docstring"""






					__magic_name__ :							Dict		       =							create_text_encoder_lora_attn_procs(UpperCAmelCase	)
					__magic_name__ :							str		       =							AttnProcsLayers(UpperCAmelCase	)
					return text_encoder_lora_layers







def 			lowerCAmelCase  (     UpperCAmelCase,     UpperCAmelCase=False	)  ->int:






					"""simple docstring"""






					for _, attn_proc in text_lora_attn_procs.items():
										# set up.weights
										for layer_name, layer_module in attn_proc.named_modules():
															if layer_name.endswith('''_lora'''	):
																				__magic_name__ :							List[Any]		       =							(
																				    torch.randn_like(layer_module.up.weight	)
																				    if randn_weight
																				    else torch.zeros_like(layer_module.up.weight	)
																				)
																				__magic_name__ :							List[Any]		       =							torch.nn.Parameter(UpperCAmelCase	)





class 		A__    (       unittest.TestCase			):






	def 			lowercase				(     self						)				->       Optional[Any]:




						"""simple docstring"""






						torch.manual_seed(0						)
						__magic_name__ :							int		       =							UNetaDConditionModel(
						    block_out_channels=(32, 64)    ,					layers_per_block=2    ,					sample_size=32    ,					in_channels=4    ,					out_channels=4    ,					down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')    ,					up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')    ,					cross_attention_dim=32    ,					)
						__magic_name__ :							List[str]		       =							DDIMScheduler(
						    beta_start=0.0_0_0_8_5    ,					beta_end=0.0_1_2    ,					beta_schedule='''scaled_linear'''    ,					clip_sample=lowerCamelCase    ,					set_alpha_to_one=lowerCamelCase    ,					steps_offset=1    ,					)
						torch.manual_seed(0						)
						__magic_name__ :							Optional[int]		       =							AutoencoderKL(
						    block_out_channels=[32, 64]    ,					in_channels=3    ,					out_channels=3    ,					down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']    ,					up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']    ,					latent_channels=4    ,					)
						__magic_name__ :							Optional[Any]		       =							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=1000    ,					)
						__magic_name__ :							Optional[Any]		       =							CLIPTextModel(lowerCamelCase						)
						__magic_name__ :							Dict		       =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''						)

						__magic_name__ :							Union[str, Any]		       =							create_unet_lora_layers(lowerCamelCase						)
						__magic_name__ :							List[str]		       =							create_text_encoder_lora_layers(lowerCamelCase						)

						__magic_name__ :							int		       =							{
						    '''unet''': unet,
						    '''scheduler''': scheduler,
						    '''vae''': vae,
						    '''text_encoder''': text_encoder,
						    '''tokenizer''': tokenizer,
						    '''safety_checker''': None,
						    '''feature_extractor''': None,
						}
						__magic_name__ :							Tuple		       =							{
						    '''unet_lora_layers''': unet_lora_layers,
						    '''text_encoder_lora_layers''': text_encoder_lora_layers,
						    '''unet_lora_attn_procs''': unet_lora_attn_procs,
						}
						return pipeline_components, lora_components






	def 			lowercase				(     self						)				->       int:




						"""simple docstring"""






						__magic_name__ :							str		       =							1
						__magic_name__ :							Dict		       =							10
						__magic_name__ :							Any		       =							4
						__magic_name__ :							int		       =							(32, 32)

						__magic_name__ :							List[str]		       =							torch.manual_seed(0						)
						__magic_name__ :							Dict		       =							floats_tensor((batch_size, num_channels) + sizes						)
						__magic_name__ :							Any		       =							torch.randint(1    ,					lowerCamelCase    ,					size=(batch_size, sequence_length)    ,					generator=lowerCamelCase						)

						__magic_name__ :							Any		       =							{
						    '''prompt''': '''A painting of a squirrel eating a burger''',
						    '''generator''': generator,
						    '''num_inference_steps''': 2,
						    '''guidance_scale''': 6.0,
						    '''output_type''': '''numpy''',
						}

						return noise, input_ids, pipeline_inputs

						# copied from: https://colab.research.google.com/gist/sayakpaul/df2ef6e1ae6d8c10a49d859883b10860/scratchpad.ipynb






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							Tuple		       =							77

						__magic_name__ :							Optional[int]		       =							torch.randint(2    ,					56    ,					size=(1, max_seq_length)    ,					generator=torch.manual_seed(0						)						)

						__magic_name__ :							Dict		       =							{}
						__magic_name__ :							int		       =							inputs
						return prepared_inputs






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




						"""simple docstring"""






						__magic_name__ :							Any		       =							self.get_dummy_components()
						LoraLoaderMixin.save_lora_weights(
						    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
						self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)






	def 			lowercase				(     self						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__ :							Optional[int]		       =							self.get_dummy_components()
						__magic_name__ :							List[str]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							int		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							self.get_dummy_inputs()

						__magic_name__ :							List[Any]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							str		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							str		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							str		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






	def 			lowercase				(     self						)				->       str:




						"""simple docstring"""






						__magic_name__ :							List[Any]		       =							self.get_dummy_components()
						__magic_name__ :							Optional[int]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							List[Any]		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							Any		       =							self.get_dummy_inputs()

						__magic_name__ :							int		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							Optional[Any]		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					safe_serialization=lowerCamelCase    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.safetensors'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							List[Any]		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							self.get_dummy_components()
						__magic_name__ :							Optional[Any]		       =							lora_components['''unet_lora_attn_procs''']
						__magic_name__ :							str		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							Union[str, Any]		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							Any		       =							self.get_dummy_inputs()

						__magic_name__ :							Union[str, Any]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							List[str]		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											__magic_name__ :							Union[str, Any]		       =							sd_pipe.unet
											unet.set_attn_processor(lowerCamelCase						)
											unet.save_attn_procs(lowerCamelCase						)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Tuple		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							List[Any]		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






	def 			lowercase				(     self						)				->       Optional[Any]:




						"""simple docstring"""






						__magic_name__ :							List[Any]		       =							self.get_dummy_components()
						__magic_name__ :							Tuple		       =							StableDiffusionPipeline(**lowerCamelCase						)

						__magic_name__ :							Dict		       =							self.get_dummy_tokens()

						# inference without lora
						__magic_name__ :							List[Any]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_without_lora.shape == (1, 77, 32)

						# create lora_attn_procs with zeroed out up.weights
						__magic_name__ :							Dict		       =							create_text_encoder_lora_attn_procs(pipe.text_encoder						)
						set_lora_up_weights(lowerCamelCase    ,					randn_weight=lowerCamelCase						)

						# monkey patch
						pipe._modify_text_encoder(lowerCamelCase						)

						# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
						del text_attn_procs
						gc.collect()

						# inference with lora
						__magic_name__ :							int		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_with_lora.shape == (1, 77, 32)

						assert torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "lora_up_weight are all zero, so the lora outputs should be the same to without lora outputs"

						# create lora_attn_procs with randn up.weights
						__magic_name__ :							int		       =							create_text_encoder_lora_attn_procs(pipe.text_encoder						)
						set_lora_up_weights(lowerCamelCase    ,					randn_weight=lowerCamelCase						)

						# monkey patch
						pipe._modify_text_encoder(lowerCamelCase						)

						# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
						del text_attn_procs
						gc.collect()

						# inference with lora
						__magic_name__ :							Optional[int]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_with_lora.shape == (1, 77, 32)

						assert not torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "lora_up_weight are not zero, so the lora outputs should be different to without lora outputs"






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							int		       =							self.get_dummy_components()
						__magic_name__ :							Optional[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)

						__magic_name__ :							Union[str, Any]		       =							self.get_dummy_tokens()

						# inference without lora
						__magic_name__ :							Union[str, Any]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_without_lora.shape == (1, 77, 32)

						# create lora_attn_procs with randn up.weights
						__magic_name__ :							Tuple		       =							create_text_encoder_lora_attn_procs(pipe.text_encoder						)
						set_lora_up_weights(lowerCamelCase    ,					randn_weight=lowerCamelCase						)

						# monkey patch
						pipe._modify_text_encoder(lowerCamelCase						)

						# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
						del text_attn_procs
						gc.collect()

						# inference with lora
						__magic_name__ :							Optional[int]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_with_lora.shape == (1, 77, 32)

						assert not torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "lora outputs should be different to without lora outputs"

						# remove monkey patch
						pipe._remove_text_encoder_monkey_patch()

						# inference with removed lora
						__magic_name__ :							Optional[Any]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_without_lora_removed.shape == (1, 77, 32)

						assert torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "remove lora monkey patch should restore the original outputs"






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							str		       =							self.get_dummy_components()
						__magic_name__ :							List[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							str		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							List[str]		       =							self.get_dummy_inputs()

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							int		       =							lora_images[0, -3:, -3:, -1]

						__magic_name__ :							int		       =							sd_pipe(**lowerCamelCase    ,					cross_attention_kwargs={'''scale''': 0.5}						).images
						__magic_name__ :							str		       =							lora_images_with_scale[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(
						    torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






	def 			lowercase				(     self						)				->       List[Any]:




						"""simple docstring"""






						with tempfile.TemporaryDirectory() as tmpdirname:
											self.create_lora_weight_file(lowerCamelCase						)

											__magic_name__ :							Tuple		       =							self.get_dummy_components()
											__magic_name__ :							str		       =							StableDiffusionPipeline(**lowerCamelCase						)
											__magic_name__ :							Optional[int]		       =							sd_pipe.to(lowerCamelCase						)
											sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

											# check if vanilla attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					self.assertIsInstance(module.processor    ,					(AttnProcessor, AttnProcessor2_0)						)

            # load LoRA weight file
											sd_pipe.load_lora_weights(lowerCamelCase						)

											# check if lora attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					__magic_name__ :							Dict		       =							(
																					    LoRAAttnProcessor2_0 if hasattr(lowerCamelCase    ,					'''scaled_dot_product_attention'''						) else LoRAAttnProcessor
																					)
																					self.assertIsInstance(module.processor    ,					lowerCamelCase						)






	@unittest.skipIf(torch_device != '''cuda'''    ,					'''This test is supposed to run on GPU'''						)
	def 			lowercase				(     self						)				->       Optional[Any]:




						"""simple docstring"""






						with tempfile.TemporaryDirectory() as tmpdirname:
											self.create_lora_weight_file(lowerCamelCase						)

											__magic_name__ :							Any		       =							self.get_dummy_components()
											__magic_name__ :							List[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)
											__magic_name__ :							Optional[int]		       =							sd_pipe.to(lowerCamelCase						)
											sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

											# enable XFormers
											sd_pipe.enable_xformers_memory_efficient_attention()

											# check if xFormers attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					self.assertIsInstance(module.processor    ,					lowerCamelCase						)

            # load LoRA weight file
											sd_pipe.load_lora_weights(lowerCamelCase						)

											# check if lora attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					self.assertIsInstance(module.processor    ,					lowerCamelCase						)






	@unittest.skipIf(torch_device != '''cuda'''    ,					'''This test is supposed to run on GPU'''						)
	def 			lowercase				(     self						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							self.get_dummy_components()
						__magic_name__ :							Optional[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							Union[str, Any]		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							List[Any]		       =							self.get_dummy_inputs()

						# enable XFormers
						sd_pipe.enable_xformers_memory_efficient_attention()

						__magic_name__ :							List[str]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							Union[str, Any]		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							Union[str, Any]		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)









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

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


lowercase_		=							logging.get_logger(__name__)

lowercase_		=							{
    '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json''',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}





class 		A__    (       __SCREAMING_SNAKE_CASE			):
	lowerCamelCase__		:			Optional[int]       ="m2m_100"
	lowerCamelCase__		:			List[str]       =["past_key_values"]
	lowerCamelCase__		:			Union[str, Any]       ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}






	def __init__(     self    ,					lowerCamelCase=128112    ,					lowerCamelCase=1024    ,					lowerCamelCase=12    ,					lowerCamelCase=4096    ,					lowerCamelCase=16    ,					lowerCamelCase=12    ,					lowerCamelCase=4096    ,					lowerCamelCase=16    ,					lowerCamelCase=0.0_5    ,					lowerCamelCase=0.0_5    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase="relu"    ,					lowerCamelCase=1024    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.0    ,					lowerCamelCase=0.0_2    ,					lowerCamelCase=2    ,					lowerCamelCase=True    ,					lowerCamelCase=1    ,					lowerCamelCase=0    ,					lowerCamelCase=2    ,					**lowerCamelCase    ,					)				->       List[str]:




						"""simple docstring"""






						__magic_name__ :							List[Any]		       =							vocab_size
						__magic_name__ :							str		       =							max_position_embeddings
						__magic_name__ :							Union[str, Any]		       =							d_model
						__magic_name__ :							Dict		       =							encoder_ffn_dim
						__magic_name__ :							List[Any]		       =							encoder_layers
						__magic_name__ :							Optional[int]		       =							encoder_attention_heads
						__magic_name__ :							Dict		       =							decoder_ffn_dim
						__magic_name__ :							List[str]		       =							decoder_layers
						__magic_name__ :							Optional[Any]		       =							decoder_attention_heads
						__magic_name__ :							List[Any]		       =							dropout
						__magic_name__ :							List[str]		       =							attention_dropout
						__magic_name__ :							Optional[int]		       =							activation_dropout
						__magic_name__ :							Dict		       =							activation_function
						__magic_name__ :							Tuple		       =							init_std
						__magic_name__ :							Optional[int]		       =							encoder_layerdrop
						__magic_name__ :							int		       =							decoder_layerdrop
						__magic_name__ :							Union[str, Any]		       =							use_cache
						__magic_name__ :							Optional[int]		       =							encoder_layers
						__magic_name__ :							Dict		       =							scale_embedding  # scale factor will be sqrt(d_model) if True

						super().__init__(
						    pad_token_id=lowerCamelCase    ,					bos_token_id=lowerCamelCase    ,					eos_token_id=lowerCamelCase    ,					is_encoder_decoder=lowerCamelCase    ,					decoder_start_token_id=lowerCamelCase    ,					**lowerCamelCase    ,					)





class 		A__    (       __SCREAMING_SNAKE_CASE			):






	@property
	def 			lowercase				(     self						)				->       Mapping[str, Mapping[int, str]]:




						"""simple docstring"""






						__magic_name__ :							Dict		       =							OrderedDict(
						    [
						        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
						        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
						    ]						)

						if self.use_past:
											__magic_name__ :							Optional[Any]		       =							{0: '''batch'''}
											__magic_name__ :							Any		       =							{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
						else:
											__magic_name__ :							Union[str, Any]		       =							{0: '''batch''', 1: '''decoder_sequence'''}
											__magic_name__ :							Optional[Any]		       =							{0: '''batch''', 1: '''decoder_sequence'''}

						if self.use_past:
											self.fill_with_past_key_values_(lowerCamelCase    ,					direction='''inputs'''						)
						return common_inputs






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase = -1    ,					lowerCamelCase = -1    ,					lowerCamelCase = False    ,					lowerCamelCase = None    ,					)				->       Mapping[str, Any]:




						"""simple docstring"""






						__magic_name__ :							int		       =							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
						__magic_name__ :							str		       =							tokenizer.num_special_tokens_to_add(lowerCamelCase						)
						__magic_name__ :							Optional[Any]		       =							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
						__magic_name__ :							Optional[Any]		       =							[''' '''.join([tokenizer.unk_token]						) * seq_length] * batch_size
						__magic_name__ :							Any		       =							dict(tokenizer(lowerCamelCase    ,					return_tensors=lowerCamelCase						)						)
						return common_inputs






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase = -1    ,					lowerCamelCase = -1    ,					lowerCamelCase = False    ,					lowerCamelCase = None    ,					)				->       Mapping[str, Any]:




						"""simple docstring"""






						__magic_name__ :							Any		       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)

						# Generate decoder inputs
						__magic_name__ :							int		       =							seq_length if not self.use_past else 1
						__magic_name__ :							Optional[Any]		       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)
						__magic_name__ :							List[str]		       =							{F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
						__magic_name__ :							Optional[Any]		       =							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
											__magic_name__       ,			__magic_name__ :							List[Any]		       =							common_inputs['''input_ids'''].shape
											__magic_name__ :							List[str]		       =							common_inputs['''decoder_input_ids'''].shape[1]
											__magic_name__       ,			__magic_name__ :							Any		       =							self.num_attention_heads
											__magic_name__ :							Optional[int]		       =							(
											    batch,
											    num_encoder_attention_heads,
											    encoder_seq_length,
											    self._config.hidden_size // num_encoder_attention_heads,
											)
											__magic_name__ :							Tuple		       =							decoder_seq_length + 3
											__magic_name__ :							str		       =							(
											    batch,
											    num_decoder_attention_heads,
											    decoder_past_length,
											    self._config.hidden_size // num_decoder_attention_heads,
											)

											__magic_name__ :							int		       =							torch.cat(
											    [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase    ,					lowerCamelCase						)]    ,					dim=1						)

											__magic_name__ :							List[str]		       =							[]
											# If the number of encoder and decoder layers are present in the model configuration, both are considered
											__magic_name__       ,			__magic_name__ :							int		       =							self.num_layers
											__magic_name__ :							List[Any]		       =							min(lowerCamelCase    ,					lowerCamelCase						)
											__magic_name__ :							int		       =							max(lowerCamelCase    ,					lowerCamelCase						) - min_num_layers
											__magic_name__ :							Optional[int]		       =							'''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.
											__magic_name__ :							Tuple		       =							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





	lowerCamelCase__		:			List[str]       =_generate_dummy_inputs_for_default_and_seqaseq_lm



