
'''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_					(lowerCAmelCase__      ):
       __lowerCAmelCase      :			Dict      = """bigbird_pegasus"""
       __lowerCAmelCase      :			Any      = ["""past_key_values"""]
       __lowerCAmelCase      :			Tuple      = {
           """num_attention_heads""": """encoder_attention_heads""",
           """hidden_size""": """d_model""",
           """attention_probs_dropout_prob""": """attention_dropout""",
       }




       def __init__(       self				,				snake_case_=9_6_1_0_3				,				snake_case_=4_0_9_6				,				snake_case_=1_6				,				snake_case_=4_0_9_6				,				snake_case_=1_6				,				snake_case_=1_6				,				snake_case_=4_0_9_6				,				snake_case_=1_6				,				snake_case_=0.0				,				snake_case_=0.0				,				snake_case_=True				,				snake_case_=True				,				snake_case_="gelu_new"				,				snake_case_=1_0_2_4				,				snake_case_=0.1				,				snake_case_=0.0				,				snake_case_=0.0				,				snake_case_=0.02				,				snake_case_=2				,				snake_case_=0.0				,				snake_case_=True				,				snake_case_=0				,				snake_case_=2				,				snake_case_=1				,				snake_case_="block_sparse"				,				snake_case_=6_4				,				snake_case_=3				,				snake_case_=False				,				**snake_case_				,				):
         _lowerCAmelCase    :					Tuple							=   vocab_size
         _lowerCAmelCase    :					Optional[int]							=   max_position_embeddings
         _lowerCAmelCase    :					List[Any]							=   d_model
         _lowerCAmelCase    :					Tuple							=   encoder_ffn_dim
         _lowerCAmelCase    :					List[Any]							=   encoder_layers
         _lowerCAmelCase    :					str							=   encoder_attention_heads
         _lowerCAmelCase    :					str							=   decoder_ffn_dim
         _lowerCAmelCase    :					Dict							=   decoder_layers
         _lowerCAmelCase    :					str							=   decoder_attention_heads
         _lowerCAmelCase    :					Tuple							=   dropout
         _lowerCAmelCase    :					int							=   attention_dropout
         _lowerCAmelCase    :					Optional[Any]							=   activation_dropout
         _lowerCAmelCase    :					Tuple							=   activation_function
         _lowerCAmelCase    :					Optional[Any]							=   init_std
         _lowerCAmelCase    :					List[Any]							=   encoder_layerdrop
         _lowerCAmelCase    :					List[str]							=   decoder_layerdrop
         _lowerCAmelCase    :					int							=   classifier_dropout
         _lowerCAmelCase    :					Tuple							=   use_cache
         _lowerCAmelCase    :					List[str]							=   encoder_layers
         _lowerCAmelCase    :					Optional[int]							=   scale_embedding  # scale factor will be sqrt(d_model) if True

         # extra config
         _lowerCAmelCase    :					Optional[int]							=   attention_type
         _lowerCAmelCase    :					Dict							=   block_size
         _lowerCAmelCase    :					Optional[int]							=   num_random_blocks
         _lowerCAmelCase    :					Optional[int]							=   use_bias

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



class 					a_					(lowerCAmelCase__      ):




       @property
       def 							__UpperCamelCase    (       self					):




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

           if self.use_past:
             _lowerCAmelCase    :					Optional[int]							=   {0: '''batch'''}
             _lowerCAmelCase    :					Any							=   {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
           else:
             _lowerCAmelCase    :					List[Any]							=   {0: '''batch''', 1: '''decoder_sequence'''}
             _lowerCAmelCase    :					int							=   {0: '''batch''', 1: '''decoder_sequence'''}

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




         if self.task in ["default", "seq2seq-lm"]:
           _lowerCAmelCase    :					Any							=   super().outputs
         else:
           _lowerCAmelCase    :					Any							=   super(_A				,				self					).outputs
           if self.use_past:
             _lowerCAmelCase    :					Union[str, Any]							=   self.num_layers
             for i in range(_A					):
               _lowerCAmelCase    :					Optional[Any]							=   {0: '''batch''', 2: '''past_sequence + sequence'''}
               _lowerCAmelCase    :					Optional[int]							=   {0: '''batch''', 2: '''past_sequence + sequence'''}
         return common_outputs




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_ = -1				,				snake_case_ = -1				,				snake_case_ = False				,				snake_case_ = None				,				):
         _lowerCAmelCase    :					List[str]							=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _A				,				_A				,				_A				,				_A				,				_A					)

         # Generate decoder inputs
         _lowerCAmelCase    :					Optional[Any]							=   seq_length if not self.use_past else 1
         _lowerCAmelCase    :					Tuple							=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _A				,				_A				,				_A				,				_A				,				_A					)
         _lowerCAmelCase    :					int							=   {f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
         _lowerCAmelCase    :					Union[str, Any]							=   dict(**_A				,				**_A					)

         if self.use_past:
           if not is_torch_available():
             raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""					)
           else:
             import torch
           _lowerCAmelCase    :					int							=   common_inputs['''input_ids'''].shape
           _lowerCAmelCase    :					List[str]							=   common_inputs['''decoder_input_ids'''].shape[1]
           _lowerCAmelCase    :					Optional[Any]							=   self.num_attention_heads
           _lowerCAmelCase    :					List[Any]							=   (
               batch,
               num_encoder_attention_heads,
               encoder_seq_length,
               self._config.hidden_size // num_encoder_attention_heads,
           )
           _lowerCAmelCase    :					List[Any]							=   decoder_seq_length + 3
           _lowerCAmelCase    :					List[str]							=   (
               batch,
               num_decoder_attention_heads,
               decoder_past_length,
               self._config.hidden_size // num_decoder_attention_heads,
           )

           _lowerCAmelCase    :					List[str]							=   torch.cat(
               [common_inputs["""decoder_attention_mask"""], torch.ones(_A				,				_A					)]				,				dim=1					)

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

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




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_ = -1				,				snake_case_ = -1				,				snake_case_ = False				,				snake_case_ = None				,				):
         _lowerCAmelCase    :					str							=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _A				,				_A				,				_A				,				_A				,				_A					)

         if self.use_past:
           if not is_torch_available():
             raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""					)
           else:
             import torch
           _lowerCAmelCase    :					str							=   common_inputs['''input_ids'''].shape
           # Not using the same length for past_key_values
           _lowerCAmelCase    :					int							=   seqlen + 2
           _lowerCAmelCase    :					Any							=   self.num_layers
           _lowerCAmelCase    :					Optional[Any]							=   self.num_attention_heads
           _lowerCAmelCase    :					List[Any]							=   (
               batch,
               num_encoder_attention_heads,
               past_key_values_length,
               self._config.hidden_size // num_encoder_attention_heads,
           )

           _lowerCAmelCase    :					Optional[int]							=   common_inputs['''attention_mask'''].dtype
           _lowerCAmelCase    :					Union[str, Any]							=   torch.cat(
               [common_inputs["""attention_mask"""], torch.ones(_A				,				_A				,				dtype=_A					)]				,				dim=1					)
           _lowerCAmelCase    :					Optional[int]							=   [
               (torch.zeros(_A					), torch.zeros(_A					)) for _ in range(_A					)
           ]
         return common_inputs




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_ = -1				,				snake_case_ = -1				,				snake_case_ = False				,				snake_case_ = None				,				):
         _lowerCAmelCase    :					List[str]							=   compute_effective_axis_dimension(
             _A				,				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    :					Optional[int]							=   tokenizer.num_special_tokens_to_add(_A					)
         _lowerCAmelCase    :					int							=   compute_effective_axis_dimension(
             _A				,				fixed_dimension=OnnxConfig.default_fixed_sequence				,				num_token_to_add=_A					)

         # Generate dummy inputs according to compute batch and sequence
         _lowerCAmelCase    :					List[Any]							=   [''' '''.join([tokenizer.unk_token]					) * seq_length] * batch_size
         _lowerCAmelCase    :					Tuple							=   dict(tokenizer(_A				,				return_tensors=_A					)					)
         return common_inputs




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_ = -1				,				snake_case_ = -1				,				snake_case_ = False				,				snake_case_ = None				,				):




         if self.task in ["default", "seq2seq-lm"]:
           _lowerCAmelCase    :					List[str]							=   self._generate_dummy_inputs_for_default_and_seqaseq_lm(
               _A				,				batch_size=_A				,				seq_length=_A				,				is_pair=_A				,				framework=_A					)

         elif self.task == "causal-lm":
           _lowerCAmelCase    :					List[Any]							=   self._generate_dummy_inputs_for_causal_lm(
               _A				,				batch_size=_A				,				seq_length=_A				,				is_pair=_A				,				framework=_A					)
         else:
           _lowerCAmelCase    :					List[Any]							=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               _A				,				batch_size=_A				,				seq_length=_A				,				is_pair=_A				,				framework=_A					)

         return common_inputs







       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_					):




         if self.task in ["default", "seq2seq-lm"]:
           _lowerCAmelCase    :					int							=   super()._flatten_past_key_values_(_A				,				_A				,				_A				,				_A					)
         else:
           _lowerCAmelCase    :					List[str]							=   super(_A				,				self					)._flatten_past_key_values_(
               _A				,				_A				,				_A				,				_A					)




'''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 					a_					(unittest.TestCase      ):




       def __init__(       self				,				snake_case_				,				snake_case_=7				,				snake_case_=3				,				snake_case_=1_8				,				snake_case_=3_0				,				snake_case_=4_0_0				,				snake_case_=True				,				snake_case_=None				,				snake_case_=0.875				,				snake_case_=True				,				snake_case_=[0.5, 0.5, 0.5]				,				snake_case_=[0.5, 0.5, 0.5]				,				):
         _lowerCAmelCase    :					List[str]							=   size if size is not None else {"""shortest_edge""": 2_0}
         _lowerCAmelCase    :					Tuple							=   parent
         _lowerCAmelCase    :					List[str]							=   batch_size
         _lowerCAmelCase    :					List[str]							=   num_channels
         _lowerCAmelCase    :					Tuple							=   image_size
         _lowerCAmelCase    :					Union[str, Any]							=   min_resolution
         _lowerCAmelCase    :					Tuple							=   max_resolution
         _lowerCAmelCase    :					List[Any]							=   do_resize
         _lowerCAmelCase    :					Dict							=   size
         _lowerCAmelCase    :					str							=   crop_pct
         _lowerCAmelCase    :					List[str]							=   do_normalize
         _lowerCAmelCase    :					Any							=   image_mean
         _lowerCAmelCase    :					Any							=   image_std







       def 							__UpperCamelCase    (       self					):
         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 					a_					(_a							,     unittest.TestCase      ):
       __lowerCAmelCase      :			Dict      = ConvNextImageProcessor if is_vision_available() else None




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					int							=   ConvNextImageProcessingTester(self					)




       @property
       def 							__UpperCamelCase    (       self					):
         return self.image_processor_tester.prepare_image_processor_dict()




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   self.image_processing_class(**self.image_processor_dict					)
         self.assertTrue(hasattr(snake_case_				,				"""do_resize"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""size"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""crop_pct"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""do_normalize"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""image_mean"""					)					)
         self.assertTrue(hasattr(snake_case_				,				"""image_std"""					)					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Any							=   self.image_processing_class.from_dict(self.image_processor_dict					)
         self.assertEqual(image_processor.size				,				{"""shortest_edge""": 2_0}					)

         _lowerCAmelCase    :					List[Any]							=   self.image_processing_class.from_dict(self.image_processor_dict				,				size=4_2					)
         self.assertEqual(image_processor.size				,				{"""shortest_edge""": 4_2}					)




       def 							__UpperCamelCase    (       self					):
         pass




       def 							__UpperCamelCase    (       self					):
         # Initialize image_processing
         _lowerCAmelCase    :					str							=   self.image_processing_class(**self.image_processor_dict					)
         # create random PIL images
         _lowerCAmelCase    :					str							=   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
         _lowerCAmelCase    :					Optional[int]							=   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
         _lowerCAmelCase    :					Optional[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["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)




       def 							__UpperCamelCase    (       self					):
         # Initialize image_processing
         _lowerCAmelCase    :					int							=   self.image_processing_class(**self.image_processor_dict					)
         # create random numpy tensors
         _lowerCAmelCase    :					Any							=   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
         _lowerCAmelCase    :					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["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)

         # Test batched
         _lowerCAmelCase    :					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["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)







       def 							__UpperCamelCase    (       self					):
         # Initialize image_processing
         _lowerCAmelCase    :					Tuple							=   self.image_processing_class(**self.image_processor_dict					)
         # create random PyTorch tensors
         _lowerCAmelCase    :					List[str]							=   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
         _lowerCAmelCase    :					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["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)

         # Test batched
         _lowerCAmelCase    :					Optional[int]							=   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["""shortest_edge"""],
                 self.image_processor_tester.size["""shortest_edge"""],
             )				,				)


