


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


__SCREAMING_SNAKE_CASE				:							Dict		       =		logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE				:							int		       =		{
    '''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 				__lowerCAmelCase      (      lowercase	):

						"""simple docstring"""






						_UpperCAmelCase		:					Any      	="bigbird_pegasus"
						_UpperCAmelCase		:					Any      	=["past_key_values"]
						_UpperCAmelCase		:					Any      	={
						    "num_attention_heads": "encoder_attention_heads",
						    "hidden_size": "d_model",
						    "attention_probs_dropout_prob": "attention_dropout",
						}






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

													# extra config
													A_ 						=		attention_type
													A_ 						=		block_size
													A_ 						=		num_random_blocks
													A_ 						=		use_bias

													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 				__lowerCAmelCase      (      lowercase	):

						"""simple docstring"""






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

																				if self.use_past:
																											A_ 						=		{0: "batch"}
																											A_ 						=		{0: "batch", 1: "past_decoder_sequence + sequence"}
																				else:
																											A_ 						=		{0: "batch", 1: "decoder_sequence"}
																											A_ 						=		{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.
																				A_ 						=		OrderedDict(
																				    [
																				        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
																				        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
																				    ]  )
																				if self.use_past:
																											A_						,				A_ 						=		self.num_layers
																											for i in range(lowerCAmelCase  ):
																																		A_ 						=		{0: "batch", 2: "past_sequence + sequence"}
																																		A_ 						=		{0: "batch", 2: "past_sequence + sequence"}
													else:
																				A_ 						=		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 							_UpperCAmelCase					(     self       :			Optional[Any]  ):
													if self.task in ["default", "seq2seq-lm"]:
																				A_ 						=		super().outputs
													else:
																				A_ 						=		super(lowerCAmelCase   ,     self  ).outputs
																				if self.use_past:
																											A_						,				A_ 						=		self.num_layers
																											for i in range(lowerCAmelCase  ):
																																		A_ 						=		{0: "batch", 2: "past_sequence + sequence"}
																																		A_ 						=		{0: "batch", 2: "past_sequence + sequence"}
													return common_outputs






						def 							_UpperCAmelCase					(     self       :			Tuple   ,     lowerCAmelCase       :			PreTrainedTokenizer   ,     lowerCAmelCase       :			int = -1   ,     lowerCAmelCase       :			int = -1   ,     lowerCAmelCase       :			bool = False   ,     lowerCAmelCase       :			Optional[TensorType] = None   ,     ):
													A_ 						=		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
													    lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase  )

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

																				A_ 						=		torch.cat(
																				    [common_inputs["decoder_attention_mask"], torch.ones(lowerCAmelCase   ,     lowerCAmelCase  )]   ,     dim=1  )

																				A_ 						=		[]
																				# If the number of encoder and decoder layers are present in the model configuration, both are considered
																				A_						,				A_ 						=		self.num_layers
																				A_ 						=		min(lowerCAmelCase   ,     lowerCAmelCase  )
																				A_ 						=		max(lowerCAmelCase   ,     lowerCAmelCase  ) - min_num_layers
																				A_ 						=		"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.
																				A_ 						=		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       :			Union[str, Any]   ,     lowerCAmelCase       :			PreTrainedTokenizer   ,     lowerCAmelCase       :			int = -1   ,     lowerCAmelCase       :			int = -1   ,     lowerCAmelCase       :			bool = False   ,     lowerCAmelCase       :			Optional[TensorType] = None   ,     ):
													A_ 						=		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
																				A_						,				A_ 						=		common_inputs["input_ids"].shape
																				# Not using the same length for past_key_values
																				A_ 						=		seqlen + 2
																				A_						,				A_ 						=		self.num_layers
																				A_						,				A_ 						=		self.num_attention_heads
																				A_ 						=		(
																				    batch,
																				    num_encoder_attention_heads,
																				    past_key_values_length,
																				    self._config.hidden_size // num_encoder_attention_heads,
																				)

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






						def 							_UpperCAmelCase					(     self       :			int   ,     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
													A_ 						=		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
													A_ 						=		tokenizer.num_special_tokens_to_add(lowerCAmelCase  )
													A_ 						=		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
													A_ 						=		[" ".join([tokenizer.unk_token]  ) * seq_length] * batch_size
													A_ 						=		dict(tokenizer(lowerCAmelCase   ,     return_tensors=lowerCAmelCase  )  )
													return common_inputs






						def 							_UpperCAmelCase					(     self       :			List[Any]   ,     lowerCAmelCase       :			PreTrainedTokenizer   ,     lowerCAmelCase       :			int = -1   ,     lowerCAmelCase       :			int = -1   ,     lowerCAmelCase       :			bool = False   ,     lowerCAmelCase       :			Optional[TensorType] = None   ,     ):
													if self.task in ["default", "seq2seq-lm"]:
																				A_ 						=		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":
																				A_ 						=		self._generate_dummy_inputs_for_causal_lm(
																				    lowerCAmelCase   ,     batch_size=lowerCAmelCase   ,     seq_length=lowerCAmelCase   ,     is_pair=lowerCAmelCase   ,     framework=lowerCAmelCase  )
													else:
																				A_ 						=		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 							_UpperCAmelCase					(     self       :			List[Any]   ,     lowerCAmelCase       :			Optional[int]   ,     lowerCAmelCase       :			Union[str, Any]   ,     lowerCAmelCase       :			List[str]   ,     lowerCAmelCase       :			Dict  ):
													if self.task in ["default", "seq2seq-lm"]:
																				A_ 						=		super()._flatten_past_key_values_(lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase  )
													else:
																				A_ 						=		super(lowerCAmelCase   ,     self  )._flatten_past_key_values_(
																				    lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase  )









'''simple docstring'''
import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
							import tensorflow as tf

							from transformers.activations_tf import get_tf_activation






@require_tf
class 				__lowerCAmelCase      (      unittest.TestCase	):

						"""simple docstring"""






						def 							_UpperCAmelCase					(     self       :			List[Any]  ):
													A_ 						=		tf.constant([-1_00, -1.0, -0.1, 0, 0.1, 1.0, 1_0_0.0]  )
													A_ 						=		get_tf_activation("gelu"  )
													A_ 						=		get_tf_activation("gelu_10"  )

													A_ 						=		gelu(lowerCAmelCase  )
													A_ 						=		geluaa(lowerCAmelCase  )

													A_ 						=		tf.where(y_gelu_aa < 1_0.0   ,     1.0   ,     0.0  )

													self.assertEqual(tf.math.reduce_max(lowerCAmelCase  ).numpy().item()   ,     1_0.0  )
													self.assertTrue(np.allclose(y_gelu * clipped_mask   ,     y_gelu_aa * clipped_mask  )  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													get_tf_activation("gelu"  )
													get_tf_activation("gelu_10"  )
													get_tf_activation("gelu_fast"  )
													get_tf_activation("gelu_new"  )
													get_tf_activation("glu"  )
													get_tf_activation("mish"  )
													get_tf_activation("quick_gelu"  )
													get_tf_activation("relu"  )
													get_tf_activation("sigmoid"  )
													get_tf_activation("silu"  )
													get_tf_activation("swish"  )
													get_tf_activation("tanh"  )
													with self.assertRaises(lowerCAmelCase  ):
																				get_tf_activation("bogus"  )
													with self.assertRaises(lowerCAmelCase  ):
																				get_tf_activation(lowerCAmelCase  )





