




"""simple docstring"""







from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available






@dataclass
# Copied from diffusers.pipelines.stable_diffusion.__init__.StableDiffusionPipelineOutput with Stable->Alt
class  SCREAMING_SNAKE_CASE__   (   _a   ):
						_a        =  42
						_a        =  42


if is_transformers_available() and is_torch_available():
		from .modeling_roberta_series import RobertaSeriesModelWithTransformation
		from .pipeline_alt_diffusion import AltDiffusionPipeline
		from .pipeline_alt_diffusion_imgaimg import AltDiffusionImgaImgPipeline











"""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


a								=							logging.get_logger(__name__)

a								=							{
    'facebook/mbart-large-cc25': 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json',
    # See all MBART models at https://huggingface.co/models?filter=mbart
}






class  SCREAMING_SNAKE_CASE__   (   _a   ):
						_a        =  'mbart'
						_a        =  ['past_key_values']
						_a        =  {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}


						def __init__(							self			:     Union[str, Any]  ,  lowerCAmelCase			:     Optional[Any]=5_0265  ,  lowerCAmelCase			:     str=1024  ,  lowerCAmelCase			:     str=12  ,  lowerCAmelCase			:     Any=4096  ,  lowerCAmelCase			:     str=16  ,  lowerCAmelCase			:     Optional[int]=12  ,  lowerCAmelCase			:     Optional[Any]=4096  ,  lowerCAmelCase			:     int=16  ,  lowerCAmelCase			:     Union[str, Any]=0.0  ,  lowerCAmelCase			:     List[str]=0.0  ,  lowerCAmelCase			:     int=True  ,  lowerCAmelCase			:     List[Any]=True  ,  lowerCAmelCase			:     Union[str, Any]="gelu"  ,  lowerCAmelCase			:     List[Any]=1024  ,  lowerCAmelCase			:     Optional[Any]=0.1  ,  lowerCAmelCase			:     List[str]=0.0  ,  lowerCAmelCase			:     str=0.0  ,  lowerCAmelCase			:     str=0.02  ,  lowerCAmelCase			:     int=0.0  ,  lowerCAmelCase			:     int=False  ,  lowerCAmelCase			:     Dict=1  ,  lowerCAmelCase			:     Tuple=0  ,  lowerCAmelCase			:     List[str]=2  ,  lowerCAmelCase			:     Union[str, Any]=2  ,  **lowerCAmelCase			:     Any  ,  ):
										lowerCAmelCase		=			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		=			classifier_dropout
										lowerCAmelCase		=			use_cache
										lowerCAmelCase		=			encoder_layers
										lowerCAmelCase		=			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  ,  forced_eos_token_id=lowerCAmelCase  ,  **lowerCAmelCase  ,  )






class  SCREAMING_SNAKE_CASE__   (   _a   ):


						@property
						def 					__lowercase	(							self			:     List[Any]   ):
										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
						def 					__lowercase	(							self			:     Optional[Any]   ):
										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 					__lowercase	(							self			:     Tuple  ,  lowerCAmelCase			:     PreTrainedTokenizer  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     bool = False  ,  lowerCAmelCase			:     Optional[TensorType] = None  ,  ):
										lowerCAmelCase		=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    lowerCAmelCase  ,  lowerCAmelCase  ,  lowerCAmelCase  ,  lowerCAmelCase  ,  lowerCAmelCase   )

										# Generate decoder inputs
										lowerCAmelCase		=			seq_length if not self.use_past else 1
										lowerCAmelCase		=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    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 					__lowercase	(							self			:     Dict  ,  lowerCAmelCase			:     PreTrainedTokenizer  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     bool = False  ,  lowerCAmelCase			:     Optional[TensorType] = None  ,  ):
										lowerCAmelCase		=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    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 					__lowercase	(							self			:     str  ,  lowerCAmelCase			:     PreTrainedTokenizer  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     bool = False  ,  lowerCAmelCase			:     Optional[TensorType] = 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 					__lowercase	(							self			:     int  ,  lowerCAmelCase			:     PreTrainedTokenizer  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     int = -1  ,  lowerCAmelCase			:     bool = False  ,  lowerCAmelCase			:     Optional[TensorType] = 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   )

										elif self.task == "causal-lm":
														lowerCAmelCase		=			self._generate_dummy_inputs_for_causal_lm(
														    lowerCAmelCase  ,  batch_size=lowerCAmelCase  ,  seq_length=lowerCAmelCase  ,  is_pair=lowerCAmelCase  ,  framework=lowerCAmelCase   )
										else:
														lowerCAmelCase		=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
														    lowerCAmelCase  ,  batch_size=lowerCAmelCase  ,  seq_length=lowerCAmelCase  ,  is_pair=lowerCAmelCase  ,  framework=lowerCAmelCase   )

										return common_inputs




						def 					__lowercase	(							self			:     List[str]  ,  lowerCAmelCase			:     Optional[Any]  ,  lowerCAmelCase			:     List[Any]  ,  lowerCAmelCase			:     List[Any]  ,  lowerCAmelCase			:     int   ):
										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   )





