






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


__lowerCAmelCase							=logging.get_logger(__name__)

__lowerCAmelCase							={
    "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    _snake_case   (				snake_case					):

					"""simple docstring"""





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




					def __init__(     self   , UpperCAmelCase__=9_6103   , UpperCAmelCase__=4096   , UpperCAmelCase__=16   , UpperCAmelCase__=4096   , UpperCAmelCase__=16   , UpperCAmelCase__=16   , UpperCAmelCase__=4096   , UpperCAmelCase__=16   , UpperCAmelCase__=0.0   , UpperCAmelCase__=0.0   , UpperCAmelCase__=True   , UpperCAmelCase__=True   , UpperCAmelCase__="gelu_new"   , UpperCAmelCase__=1024   , UpperCAmelCase__=0.1   , UpperCAmelCase__=0.0   , UpperCAmelCase__=0.0   , UpperCAmelCase__=0.0_2   , UpperCAmelCase__=2   , UpperCAmelCase__=0.0   , UpperCAmelCase__=True   , UpperCAmelCase__=0   , UpperCAmelCase__=2   , UpperCAmelCase__=1   , UpperCAmelCase__="block_sparse"   , UpperCAmelCase__=64   , UpperCAmelCase__=3   , UpperCAmelCase__=False   , **UpperCAmelCase__   , )      ->	str:
										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=UpperCAmelCase__   , bos_token_id=UpperCAmelCase__   , eos_token_id=UpperCAmelCase__   , is_encoder_decoder=UpperCAmelCase__   , decoder_start_token_id=UpperCAmelCase__   , **UpperCAmelCase__   , )



class    _snake_case   (				snake_case					):

					"""simple docstring"""





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




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = False   , UpperCAmelCase__ = None   , )      ->	Mapping[str, Any]:
										a_									=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )

										# Generate decoder inputs
										a_									=   seq_length if not self.use_past else 1
										a_									=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )
										a_									=   {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
										a_									=   dict(**UpperCAmelCase__   , **UpperCAmelCase__     )

										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(UpperCAmelCase__   , UpperCAmelCase__     )]   , 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(UpperCAmelCase__   , UpperCAmelCase__     )
															a_									=   max(UpperCAmelCase__   , UpperCAmelCase__     ) - min_num_layers
															a_									=   'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

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




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = False   , UpperCAmelCase__ = None   , )      ->	Mapping[str, Any]:
										a_									=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )

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




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = False   , UpperCAmelCase__ = 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
										a_									=   compute_effective_axis_dimension(
										    UpperCAmelCase__   , 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(UpperCAmelCase__     )
										a_									=   compute_effective_axis_dimension(
										    UpperCAmelCase__   , fixed_dimension=OnnxConfig.default_fixed_sequence   , num_token_to_add=UpperCAmelCase__     )

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




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = -1   , UpperCAmelCase__ = False   , UpperCAmelCase__ = None   , )      ->	Mapping[str, Any]:
										if self.task in ["default", "seq2seq-lm"]:
															a_									=   self._generate_dummy_inputs_for_default_and_seqaseq_lm(
															    UpperCAmelCase__   , batch_size=UpperCAmelCase__   , seq_length=UpperCAmelCase__   , is_pair=UpperCAmelCase__   , framework=UpperCAmelCase__     )

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

										return common_inputs




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	str:
										if self.task in ["default", "seq2seq-lm"]:
															a_									=   super()._flatten_past_key_values_(UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )
										else:
															a_									=   super(UpperCAmelCase__   , self     )._flatten_past_key_values_(
															    UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )









'''simple docstring'''
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 ConvNextImageProcessor


class    _snake_case   (				unittest.TestCase					):

					"""simple docstring"""





					def __init__(     self   , UpperCAmelCase__   , UpperCAmelCase__=7   , UpperCAmelCase__=3   , UpperCAmelCase__=18   , UpperCAmelCase__=30   , UpperCAmelCase__=400   , UpperCAmelCase__=True   , UpperCAmelCase__=None   , UpperCAmelCase__=0.8_7_5   , UpperCAmelCase__=True   , UpperCAmelCase__=[0.5, 0.5, 0.5]   , UpperCAmelCase__=[0.5, 0.5, 0.5]   , )      ->	Optional[int]:
										a_									=   size if size is not None else {'shortest_edge': 20}
										a_									=   parent
										a_									=   batch_size
										a_									=   num_channels
										a_									=   image_size
										a_									=   min_resolution
										a_									=   max_resolution
										a_									=   do_resize
										a_									=   size
										a_									=   crop_pct
										a_									=   do_normalize
										a_									=   image_mean
										a_									=   image_std




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[str]:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "crop_pct": self.crop_pct,
										}



@require_torch
@require_vision
class    _snake_case   (				snake_case     ,					unittest.TestCase					):

					"""simple docstring"""





					_UpperCamelCase		 =      ConvNextImageProcessor if is_vision_available() else None




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_									=   ConvNextImageProcessingTester(self     )




					@property
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										return self.image_processor_tester.prepare_image_processor_dict()




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_resize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'size'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'crop_pct'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_normalize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_mean'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_std'     )     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_									=   self.image_processing_class.from_dict(self.image_processor_dict     )
										self.assertEqual(image_processor.size   , {'shortest_edge': 20}     )

										a_									=   self.image_processing_class.from_dict(self.image_processor_dict   , size=42     )
										self.assertEqual(image_processor.size   , {'shortest_edge': 42}     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[Any]:
										pass




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PIL images
										a_									=   prepare_image_inputs(self.image_processor_tester   , equal_resolution=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , Image.Image     )

										# Test not batched input
										a_									=   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['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , 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['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random numpy tensors
										a_									=   prepare_image_inputs(self.image_processor_tester   , equal_resolution=UpperCAmelCase__   , numpify=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , np.ndarray     )

										# Test not batched input
										a_									=   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['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , 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['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PyTorch tensors
										a_									=   prepare_image_inputs(self.image_processor_tester   , equal_resolution=UpperCAmelCase__   , torchify=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , torch.Tensor     )

										# Test not batched input
										a_									=   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['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , 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['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

