





'''simple docstring'''






import warnings
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__				:   Tuple      =logging.get_logger(__name__)

A__				:   List[Any]      ={
    '''facebook/bart-large''': '''https://huggingface.co/facebook/bart-large/resolve/main/config.json''',
    # See all BART models at https://huggingface.co/models?filter=bart
}




class  UpperCAmelCase     (			snake_case_   ):
      _lowercase:			Any						     =   '''bart'''
      _lowercase:			Union[str, Any]						     =   ['''past_key_values''']
      _lowercase:			str						     =   {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}




      def __init__(			self			: Tuple			,			__snake_case			: List[str]=5_02_65			,			__snake_case			: List[str]=10_24			,			__snake_case			: Union[str, Any]=12			,			__snake_case			: Optional[Any]=40_96			,			__snake_case			: Tuple=16			,			__snake_case			: Optional[Any]=12			,			__snake_case			: List[Any]=40_96			,			__snake_case			: Dict=16			,			__snake_case			: Any=0.0			,			__snake_case			: List[str]=0.0			,			__snake_case			: Union[str, Any]="gelu"			,			__snake_case			: str=10_24			,			__snake_case			: int=0.1			,			__snake_case			: int=0.0			,			__snake_case			: List[str]=0.0			,			__snake_case			: Union[str, Any]=0.02			,			__snake_case			: List[Any]=0.0			,			__snake_case			: Optional[int]=False			,			__snake_case			: List[str]=True			,			__snake_case			: Any=3			,			__snake_case			: List[Any]=1			,			__snake_case			: List[str]=0			,			__snake_case			: Optional[Any]=2			,			__snake_case			: int=True			,			__snake_case			: List[str]=2			,			__snake_case			: Optional[int]=2			,			**__snake_case			: Union[str, Any]			,			)				->      Optional[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__(
                num_labels=__snake_case			,			pad_token_id=__snake_case			,			bos_token_id=__snake_case			,			eos_token_id=__snake_case			,			is_encoder_decoder=__snake_case			,			decoder_start_token_id=__snake_case			,			forced_eos_token_id=__snake_case			,			**__snake_case			,			)

            # ensure backward compatibility for BART CNN models
            if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated"""			,			__snake_case  ):
                  _lowerCAmelCase							     =							self.bos_token_id
                  warnings.warn(
                      f"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. "
                      """The config can simply be saved and uploaded again to be fixed."""  )




class  UpperCAmelCase     (			snake_case_   ):




      @property
      def 				lowercase__    (			self			: Dict  )				->      Mapping[str, Mapping[int, str]]:
            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_(__snake_case			,			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(__snake_case  ):
                              _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			: List[Any]  )				->      Mapping[str, Mapping[int, str]]:
            if self.task in ["default", "seq2seq-lm"]:
                  _lowerCAmelCase							     =							super().outputs
            else:
                  _lowerCAmelCase							     =							super(__snake_case			,			self  ).outputs
                  if self.use_past:
                        _lowerCAmelCase     ,							_lowerCAmelCase							     =							self.num_layers
                        for i in range(__snake_case  ):
                              _lowerCAmelCase							     =							{0: """batch""", 2: """past_sequence + sequence"""}
                              _lowerCAmelCase							     =							{0: """batch""", 2: """past_sequence + sequence"""}
            return common_outputs




      def 				lowercase__    (			self			: Optional[int]			,			__snake_case			: PreTrainedTokenizer			,			__snake_case			: int = -1			,			__snake_case			: int = -1			,			__snake_case			: bool = False			,			__snake_case			: Optional[TensorType] = None			,			)				->      Mapping[str, Any]:
            _lowerCAmelCase							     =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __snake_case			,			__snake_case			,			__snake_case			,			__snake_case			,			__snake_case  )

            # Generate decoder inputs
            _lowerCAmelCase							     =							seq_length if not self.use_past else 1
            _lowerCAmelCase							     =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __snake_case			,			__snake_case			,			__snake_case			,			__snake_case			,			__snake_case  )
            _lowerCAmelCase							     =							{f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
            _lowerCAmelCase							     =							dict(**__snake_case			,			**__snake_case  )

            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(__snake_case			,			__snake_case  )]			,			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(__snake_case			,			__snake_case  )
                  _lowerCAmelCase							     =							max(__snake_case			,			__snake_case  ) - min_num_layers
                  _lowerCAmelCase							     =							"""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

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




      def 				lowercase__    (			self			: Tuple			,			__snake_case			: PreTrainedTokenizer			,			__snake_case			: int = -1			,			__snake_case			: int = -1			,			__snake_case			: bool = False			,			__snake_case			: Optional[TensorType] = None			,			)				->      Mapping[str, Any]:
            _lowerCAmelCase							     =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __snake_case			,			__snake_case			,			__snake_case			,			__snake_case			,			__snake_case  )

            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(__snake_case			,			__snake_case			,			dtype=__snake_case  )]			,			dim=1  )
                  _lowerCAmelCase							     =							[
                      (torch.zeros(__snake_case  ), torch.zeros(__snake_case  )) for _ in range(__snake_case  )
                  ]
            return common_inputs




      def 				lowercase__    (			self			: Any			,			__snake_case			: PreTrainedTokenizer			,			__snake_case			: int = -1			,			__snake_case			: int = -1			,			__snake_case			: bool = False			,			__snake_case			: Optional[TensorType] = None			,			)				->      Mapping[str, Any]:
            # 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(
                __snake_case			,			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(__snake_case  )
            _lowerCAmelCase							     =							compute_effective_axis_dimension(
                __snake_case			,			fixed_dimension=OnnxConfig.default_fixed_sequence			,			num_token_to_add=__snake_case  )

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




      def 				lowercase__    (			self			: Dict			,			__snake_case			: PreTrainedTokenizer			,			__snake_case			: int = -1			,			__snake_case			: int = -1			,			__snake_case			: bool = False			,			__snake_case			: Optional[TensorType] = None			,			)				->      Mapping[str, Any]:
            if self.task in ["default", "seq2seq-lm"]:
                  _lowerCAmelCase							     =							self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                      __snake_case			,			batch_size=__snake_case			,			seq_length=__snake_case			,			is_pair=__snake_case			,			framework=__snake_case  )

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

            return common_inputs




      def 				lowercase__    (			self			: Union[str, Any]			,			__snake_case			: List[Any]			,			__snake_case			: str			,			__snake_case			: Optional[Any]			,			__snake_case			: int  )				->      List[Any]:
            if self.task in ["default", "seq2seq-lm"]:
                  _lowerCAmelCase							     =							super()._flatten_past_key_values_(__snake_case			,			__snake_case			,			__snake_case			,			__snake_case  )
            else:
                  _lowerCAmelCase							     =							super(__snake_case			,			self  )._flatten_past_key_values_(
                      __snake_case			,			__snake_case			,			__snake_case			,			__snake_case  )












from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
						if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')):
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
						from .pipeline_unclip import UnCLIPPipeline
						from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
						from .text_proj import UnCLIPTextProjModel




