


'''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__				(							_A  ):





							'''simple docstring'''






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







							def __init__(		self					,    lowerCamelCase_=9_61_03					,    lowerCamelCase_=40_96					,    lowerCamelCase_=16					,    lowerCamelCase_=40_96					,    lowerCamelCase_=16					,    lowerCamelCase_=16					,    lowerCamelCase_=40_96					,    lowerCamelCase_=16					,    lowerCamelCase_=0.0					,    lowerCamelCase_=0.0					,    lowerCamelCase_=True					,    lowerCamelCase_=True					,    lowerCamelCase_="gelu_new"					,    lowerCamelCase_=10_24					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.0					,    lowerCamelCase_=0.0					,    lowerCamelCase_=0.02					,    lowerCamelCase_=2					,    lowerCamelCase_=0.0					,    lowerCamelCase_=True					,    lowerCamelCase_=0					,    lowerCamelCase_=2					,    lowerCamelCase_=1					,    lowerCamelCase_="block_sparse"					,    lowerCamelCase_=64					,    lowerCamelCase_=3					,    lowerCamelCase_=False					,    **lowerCamelCase_					,    )       ->							int:
														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

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

														super().__init__(
														    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_					,    **snake_case_					,    )




class 		a__				(							_A  ):





							'''simple docstring'''






							@property
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[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__          =	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   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														if self.task in ["default", "seq2seq-lm"]:
																					lowerCAmelCase__          =	super().outputs
														else:
																					lowerCAmelCase__          =	super(snake_case_					,    self   ).outputs
																					if self.use_past:
																												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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = False					,    lowerCamelCase_ = None					,    )       ->							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__          =	common_inputs["""input_ids"""].shape
																					lowerCAmelCase__          =	common_inputs["""decoder_input_ids"""].shape[1]
																					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__          =	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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = False					,    lowerCamelCase_ = None					,    )       ->							Union[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__          =	common_inputs["""input_ids"""].shape
																					# Not using the same length for past_key_values
																					lowerCAmelCase__          =	seqlen + 2
																					lowerCAmelCase__          =	self.num_layers
																					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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = False					,    lowerCamelCase_ = None					,    )       ->							Tuple:
														# 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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = False					,    lowerCamelCase_ = None					,    )       ->							Union[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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							str:
														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_   )

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import ViTImageProcessor

class 				_UpperCamelCase (    unittest.TestCase     ):






			'''simple docstring'''

			def __init__(				self       :				Optional[int]  ,		snake_case_       :				Optional[Any]  ,		snake_case_       :				List[Any]=7  ,		snake_case_       :				int=3  ,		snake_case_       :				Tuple=18  ,		snake_case_       :				List[Any]=30  ,		snake_case_       :				Tuple=400  ,		snake_case_       :				Dict=True  ,		snake_case_       :				List[str]=None  ,		snake_case_       :				Any=True  ,		snake_case_       :				str=[0.5, 0.5, 0.5]  ,		snake_case_       :				Optional[Any]=[0.5, 0.5, 0.5]  ,		):
							UpperCamelCase_:							int		=  size if size is not None else {"""height""": 18, """width""": 18}
							UpperCamelCase_:							Any		=  parent
							UpperCamelCase_:							List[str]		=  batch_size
							UpperCamelCase_:							Union[str, Any]		=  num_channels
							UpperCamelCase_:							str		=  image_size
							UpperCamelCase_:							Optional[int]		=  min_resolution
							UpperCamelCase_:							Optional[Any]		=  max_resolution
							UpperCamelCase_:							Dict		=  do_resize
							UpperCamelCase_:							Union[str, Any]		=  size
							UpperCamelCase_:							Optional[Any]		=  do_normalize
							UpperCamelCase_:							str		=  image_mean
							UpperCamelCase_:							int		=  image_std




			def 					lowerCAmelCase__		(				self       :				Tuple					):
							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							}




@require_torch
@require_vision
class 				_UpperCamelCase (    _A   ,				unittest.TestCase     ):






			'''simple docstring'''

			__UpperCamelCase					:  str    					=       ViTImageProcessor if is_vision_available() else None

			def 					lowerCAmelCase__		(				self       :				Any					):
							UpperCamelCase_:							Tuple		=  ViTImageProcessingTester(self					)

			@property
			def 					lowerCAmelCase__		(				self       :				Union[str, Any]					):
							return self.image_processor_tester.prepare_image_processor_dict()

			def 					lowerCAmelCase__		(				self       :				int					):
							UpperCamelCase_:							Union[str, Any]		=  self.image_processing_class(**self.image_processor_dict					)
							self.assertTrue(hasattr(snake_case_  ,		"""image_mean"""					)					)
							self.assertTrue(hasattr(snake_case_  ,		"""image_std"""					)					)
							self.assertTrue(hasattr(snake_case_  ,		"""do_normalize"""					)					)
							self.assertTrue(hasattr(snake_case_  ,		"""do_resize"""					)					)
							self.assertTrue(hasattr(snake_case_  ,		"""size"""					)					)

			def 					lowerCAmelCase__		(				self       :				Optional[Any]					):
							UpperCamelCase_:							Optional[Any]		=  self.image_processing_class.from_dict(self.image_processor_dict					)
							self.assertEqual(image_processor.size  ,		{"""height""": 18, """width""": 18}					)

							UpperCamelCase_:							Union[str, Any]		=  self.image_processing_class.from_dict(self.image_processor_dict  ,		size=42					)
							self.assertEqual(image_processor.size  ,		{"""height""": 42, """width""": 42}					)

			def 					lowerCAmelCase__		(				self       :				int					):
							pass

			def 					lowerCAmelCase__		(				self       :				int					):
							# Initialize image_processing
							UpperCamelCase_:							List[str]		=  self.image_processing_class(**self.image_processor_dict					)
							# create random PIL images
							UpperCamelCase_:							List[Any]		=  prepare_image_inputs(self.image_processor_tester  ,		equal_resolution=snake_case_					)
							for image in image_inputs:
											self.assertIsInstance(snake_case_  ,		Image.Image					)

							# Test not batched input
							UpperCamelCase_:							Optional[Any]		=  image_processing(image_inputs[0]  ,		return_tensors="""pt"""					).pixel_values
							self.assertEqual(
							    encoded_images.shape  ,		(
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.size["""height"""],
							        self.image_processor_tester.size["""width"""],
							    )  ,		)

							# Test batched
							UpperCamelCase_:							List[str]		=  image_processing(snake_case_  ,		return_tensors="""pt"""					).pixel_values
							self.assertEqual(
							    encoded_images.shape  ,		(
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.size["""height"""],
							        self.image_processor_tester.size["""width"""],
							    )  ,		)

			def 					lowerCAmelCase__		(				self       :				str					):
							# Initialize image_processing
							UpperCamelCase_:							List[str]		=  self.image_processing_class(**self.image_processor_dict					)
							# create random numpy tensors
							UpperCamelCase_:							Tuple		=  prepare_image_inputs(self.image_processor_tester  ,		equal_resolution=snake_case_  ,		numpify=snake_case_					)
							for image in image_inputs:
											self.assertIsInstance(snake_case_  ,		np.ndarray					)

							# Test not batched input
							UpperCamelCase_:							Any		=  image_processing(image_inputs[0]  ,		return_tensors="""pt"""					).pixel_values
							self.assertEqual(
							    encoded_images.shape  ,		(
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.size["""height"""],
							        self.image_processor_tester.size["""width"""],
							    )  ,		)

							# Test batched
							UpperCamelCase_:							Dict		=  image_processing(snake_case_  ,		return_tensors="""pt"""					).pixel_values
							self.assertEqual(
							    encoded_images.shape  ,		(
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.size["""height"""],
							        self.image_processor_tester.size["""width"""],
							    )  ,		)




			def 					lowerCAmelCase__		(				self       :				Any					):
							# Initialize image_processing
							UpperCamelCase_:							int		=  self.image_processing_class(**self.image_processor_dict					)
							# create random PyTorch tensors
							UpperCamelCase_:							Dict		=  prepare_image_inputs(self.image_processor_tester  ,		equal_resolution=snake_case_  ,		torchify=snake_case_					)
							for image in image_inputs:
											self.assertIsInstance(snake_case_  ,		torch.Tensor					)

							# Test not batched input
							UpperCamelCase_:							List[str]		=  image_processing(image_inputs[0]  ,		return_tensors="""pt"""					).pixel_values
							self.assertEqual(
							    encoded_images.shape  ,		(
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.size["""height"""],
							        self.image_processor_tester.size["""width"""],
							    )  ,		)

							# Test batched
							UpperCamelCase_:							Any		=  image_processing(snake_case_  ,		return_tensors="""pt"""					).pixel_values
							self.assertEqual(
							    encoded_images.shape  ,		(
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.size["""height"""],
							        self.image_processor_tester.size["""width"""],
							    )  ,		)






