
'''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            = logging.get_logger(__name__)

UpperCAmelCase            = {
    "google/bigbird-pegasus-large-arxiv": (
        "https://huggingface.co/google/bigbird-pegasus-large-arxiv/resolve/main/config.json"
    ),
    "google/bigbird-pegasus-large-pubmed": (
        "https://huggingface.co/google/bigbird-pegasus-large-pubmed/resolve/main/config.json"
    ),
    "google/bigbird-pegasus-large-bigpatent": (
        "https://huggingface.co/google/bigbird-pegasus-large-bigpatent/resolve/main/config.json"
    ),
    # See all BigBirdPegasus models at https://huggingface.co/models?filter=bigbird_pegasus
}

class     a (					__magic_name__     ):
				_snake_case		     =			'''bigbird_pegasus'''
				_snake_case		     =			['''past_key_values''']
				_snake_case		     =			{
				    '''num_attention_heads''': '''encoder_attention_heads''',
				    '''hidden_size''': '''d_model''',
				    '''attention_probs_dropout_prob''': '''attention_dropout''',
				}




				def __init__(				self						:					str,     SCREAMING_SNAKE_CASE_						:					List[str]=9_61_03,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=40_96,     SCREAMING_SNAKE_CASE_						:					Tuple=16,     SCREAMING_SNAKE_CASE_						:					Any=40_96,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=16,     SCREAMING_SNAKE_CASE_						:					Any=16,     SCREAMING_SNAKE_CASE_						:					str=40_96,     SCREAMING_SNAKE_CASE_						:					Optional[Any]=16,     SCREAMING_SNAKE_CASE_						:					int=0.0,     SCREAMING_SNAKE_CASE_						:					Optional[int]=0.0,     SCREAMING_SNAKE_CASE_						:					Tuple=True,     SCREAMING_SNAKE_CASE_						:					str=True,     SCREAMING_SNAKE_CASE_						:					Optional[Any]="gelu_new",     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=10_24,     SCREAMING_SNAKE_CASE_						:					Any=0.1,     SCREAMING_SNAKE_CASE_						:					Tuple=0.0,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=0.0,     SCREAMING_SNAKE_CASE_						:					int=0.02,     SCREAMING_SNAKE_CASE_						:					str=2,     SCREAMING_SNAKE_CASE_						:					Optional[int]=0.0,     SCREAMING_SNAKE_CASE_						:					Any=True,     SCREAMING_SNAKE_CASE_						:					List[Any]=0,     SCREAMING_SNAKE_CASE_						:					Optional[Any]=2,     SCREAMING_SNAKE_CASE_						:					Tuple=1,     SCREAMING_SNAKE_CASE_						:					Any="block_sparse",     SCREAMING_SNAKE_CASE_						:					Optional[Any]=64,     SCREAMING_SNAKE_CASE_						:					Dict=3,     SCREAMING_SNAKE_CASE_						:					List[str]=False,     **SCREAMING_SNAKE_CASE_						:					str,     ):
										snake_case							:  Union[str, Any]			=     vocab_size
										snake_case							:  Dict			=     max_position_embeddings
										snake_case							:  Optional[Any]			=     d_model
										snake_case							:  str			=     encoder_ffn_dim
										snake_case							:  Tuple			=     encoder_layers
										snake_case							:  Any			=     encoder_attention_heads
										snake_case							:  str			=     decoder_ffn_dim
										snake_case							:  List[str]			=     decoder_layers
										snake_case							:  str			=     decoder_attention_heads
										snake_case							:  Union[str, Any]			=     dropout
										snake_case							:  int			=     attention_dropout
										snake_case							:  List[Any]			=     activation_dropout
										snake_case							:  Tuple			=     activation_function
										snake_case							:  List[str]			=     init_std
										snake_case							:  Union[str, Any]			=     encoder_layerdrop
										snake_case							:  List[Any]			=     decoder_layerdrop
										snake_case							:  Optional[int]			=     classifier_dropout
										snake_case							:  Optional[int]			=     use_cache
										snake_case							:  Optional[Any]			=     encoder_layers
										snake_case							:  List[Any]			=     scale_embedding  # scale factor will be sqrt(d_model) if True

										# extra config
										snake_case							:  Optional[int]			=     attention_type
										snake_case							:  int			=     block_size
										snake_case							:  str			=     num_random_blocks
										snake_case							:  int			=     use_bias

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




class     a (					__magic_name__     ):




				@property
				def 						__snake_case				(				self						:					List[str]   ):
										if self.task in ["default", "seq2seq-lm"]:
																snake_case							:  List[str]			=     OrderedDict(
																    [
																        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
																        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
																    ]   )

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

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




				def 						__snake_case				(				self						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					PreTrainedTokenizer,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					bool = False,     SCREAMING_SNAKE_CASE_						:					Optional[TensorType] = None,     ):
										snake_case							:  Tuple			=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_   )

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

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

																snake_case							:  str			=     []
																# If the number of encoder and decoder layers are present in the model configuration, both are considered
																snake_case,       snake_case							:  Optional[int]			=     self.num_layers
																snake_case							:  Dict			=     min(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_   )
																snake_case							:  List[str]			=     max(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_   ) - min_num_layers
																snake_case							:  int			=     '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

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




				def 						__snake_case				(				self						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					PreTrainedTokenizer,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					bool = False,     SCREAMING_SNAKE_CASE_						:					Optional[TensorType] = None,     ):
										snake_case							:  List[str]			=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_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
																snake_case,       snake_case							:  Any			=     common_inputs['''input_ids'''].shape
																# Not using the same length for past_key_values
																snake_case							:  str			=     seqlen + 2
																snake_case,       snake_case							:  int			=     self.num_layers
																snake_case,       snake_case							:  int			=     self.num_attention_heads
																snake_case							:  Union[str, Any]			=     (
																    batch,
																    num_encoder_attention_heads,
																    past_key_values_length,
																    self._config.hidden_size // num_encoder_attention_heads,
																)

																snake_case							:  str			=     common_inputs['''attention_mask'''].dtype
																snake_case							:  Optional[Any]			=     torch.cat(
																    [common_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     dtype=SCREAMING_SNAKE_CASE_   )],     dim=1   )
																snake_case							:  List[Any]			=     [
																    (torch.zeros(SCREAMING_SNAKE_CASE_   ), torch.zeros(SCREAMING_SNAKE_CASE_   )) for _ in range(SCREAMING_SNAKE_CASE_   )
																]
										return common_inputs




				def 						__snake_case				(				self						:					Dict,     SCREAMING_SNAKE_CASE_						:					PreTrainedTokenizer,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					bool = False,     SCREAMING_SNAKE_CASE_						:					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
										snake_case							:  List[Any]			=     compute_effective_axis_dimension(
										    SCREAMING_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
										snake_case							:  Any			=     tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE_   )
										snake_case							:  Optional[Any]			=     compute_effective_axis_dimension(
										    SCREAMING_SNAKE_CASE_,     fixed_dimension=OnnxConfig.default_fixed_sequence,     num_token_to_add=SCREAMING_SNAKE_CASE_   )

										# Generate dummy inputs according to compute batch and sequence
										snake_case							:  Optional[Any]			=     [''' '''.join([tokenizer.unk_token]   ) * seq_length] * batch_size
										snake_case							:  Optional[int]			=     dict(tokenizer(SCREAMING_SNAKE_CASE_,     return_tensors=SCREAMING_SNAKE_CASE_   )   )
										return common_inputs




				def 						__snake_case				(				self						:					Tuple,     SCREAMING_SNAKE_CASE_						:					PreTrainedTokenizer,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					int = -1,     SCREAMING_SNAKE_CASE_						:					bool = False,     SCREAMING_SNAKE_CASE_						:					Optional[TensorType] = None,     ):
										if self.task in ["default", "seq2seq-lm"]:
																snake_case							:  List[Any]			=     self._generate_dummy_inputs_for_default_and_seqaseq_lm(
																    SCREAMING_SNAKE_CASE_,     batch_size=SCREAMING_SNAKE_CASE_,     seq_length=SCREAMING_SNAKE_CASE_,     is_pair=SCREAMING_SNAKE_CASE_,     framework=SCREAMING_SNAKE_CASE_   )

										elif self.task == "causal-lm":
																snake_case							:  Dict			=     self._generate_dummy_inputs_for_causal_lm(
																    SCREAMING_SNAKE_CASE_,     batch_size=SCREAMING_SNAKE_CASE_,     seq_length=SCREAMING_SNAKE_CASE_,     is_pair=SCREAMING_SNAKE_CASE_,     framework=SCREAMING_SNAKE_CASE_   )
										else:
																snake_case							:  Optional[int]			=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
																    SCREAMING_SNAKE_CASE_,     batch_size=SCREAMING_SNAKE_CASE_,     seq_length=SCREAMING_SNAKE_CASE_,     is_pair=SCREAMING_SNAKE_CASE_,     framework=SCREAMING_SNAKE_CASE_   )

										return common_inputs




				def 						__snake_case				(				self						:					Dict,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Optional[Any]   ):
										if self.task in ["default", "seq2seq-lm"]:
																snake_case							:  str			=     super()._flatten_past_key_values_(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_   )
										else:
																snake_case							:  List[str]			=     super(SCREAMING_SNAKE_CASE_,     self   )._flatten_past_key_values_(
																    SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_   )


"""simple docstring"""


import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


lowerCAmelCase_:       List[str]			     =       logging.get_logger(__name__)






class  a__     ( _a						):
   snake_case_				=   "speech-encoder-decoder"
   snake_case_				=   True


   def __init__(  self,					**_UpperCAmelCase	):
        '''simple docstring'''



        super().__init__(**_UpperCAmelCase	)
        if "encoder" not in kwargs or "decoder" not in kwargs:
             raise ValueError(
                 F'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
                 F''' `decoder` sub-configurations are passed, but only {kwargs}'''	)

        lowercase__								=				kwargs.pop("encoder"	)
        lowercase__								=				encoder_config.pop("model_type"	)
        lowercase__								=				kwargs.pop("decoder"	)
        lowercase__								=				decoder_config.pop("model_type"	)

        lowercase__								=				AutoConfig.for_model(_UpperCAmelCase,					**_UpperCAmelCase	)
        lowercase__								=				AutoConfig.for_model(_UpperCAmelCase,					**_UpperCAmelCase	)
        lowercase__								=				True


   @classmethod
   def 						snake_case__						(  cls,					_UpperCAmelCase,					_UpperCAmelCase,					**_UpperCAmelCase	):
        '''simple docstring'''



        logger.info("Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"	)
        lowercase__								=				True
        lowercase__								=				True

        return cls(encoder=encoder_config.to_dict(),					decoder=decoder_config.to_dict(),					**_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				copy.deepcopy(self.__dict__	)
        lowercase__								=				self.encoder.to_dict()
        lowercase__								=				self.decoder.to_dict()
        lowercase__								=				self.__class__.model_type
        return output




