


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


_snake_case					=					logging.get_logger(__name__)

_snake_case					=					{
    '''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      UpperCAmelCase_ ( UpperCamelCase   ):

   '''simple docstring'''




   __A			:				int							=		"bigbird_pegasus"
   __A			:				Union[str, Any]							=		["past_key_values"]
   __A			:				int							=		{
       "num_attention_heads": "encoder_attention_heads",
       "hidden_size": "d_model",
       "attention_probs_dropout_prob": "attention_dropout",
   }






   def __init__(		self						,    __A=9_6103						,    __A=4096						,    __A=16						,    __A=4096						,    __A=16						,    __A=16						,    __A=4096						,    __A=16						,    __A=0.0						,    __A=0.0						,    __A=True						,    __A=True						,    __A="gelu_new"						,    __A=1024						,    __A=0.1						,    __A=0.0						,    __A=0.0						,    __A=0.02						,    __A=2						,    __A=0.0						,    __A=True						,    __A=0						,    __A=2						,    __A=1						,    __A="block_sparse"						,    __A=64						,    __A=3						,    __A=False						,    **__A						,    ):






     """simple docstring"""





     lowerCamelCase			: List[Any]   =    vocab_size
     lowerCamelCase			: Any   =    max_position_embeddings
     lowerCamelCase			: str   =    d_model
     lowerCamelCase			: Dict   =    encoder_ffn_dim
     lowerCamelCase			: List[str]   =    encoder_layers
     lowerCamelCase			: Union[str, Any]   =    encoder_attention_heads
     lowerCamelCase			: Optional[int]   =    decoder_ffn_dim
     lowerCamelCase			: Tuple   =    decoder_layers
     lowerCamelCase			: List[str]   =    decoder_attention_heads
     lowerCamelCase			: int   =    dropout
     lowerCamelCase			: Any   =    attention_dropout
     lowerCamelCase			: Optional[Any]   =    activation_dropout
     lowerCamelCase			: int   =    activation_function
     lowerCamelCase			: List[str]   =    init_std
     lowerCamelCase			: Tuple   =    encoder_layerdrop
     lowerCamelCase			: List[Any]   =    decoder_layerdrop
     lowerCamelCase			: Optional[int]   =    classifier_dropout
     lowerCamelCase			: Optional[Any]   =    use_cache
     lowerCamelCase			: Tuple   =    encoder_layers
     lowerCamelCase			: Optional[int]   =    scale_embedding  # scale factor will be sqrt(d_model) if True

     # extra config
     lowerCamelCase			: List[Any]   =    attention_type
     lowerCamelCase			: int   =    block_size
     lowerCamelCase			: Tuple   =    num_random_blocks
     lowerCamelCase			: Optional[Any]   =    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      UpperCAmelCase_ ( UpperCamelCase   ):

   '''simple docstring'''




   @property
   def _snake_case   (		self			):






     """simple docstring"""





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

       if self.use_past:
         lowerCamelCase			: Optional[Any]   =    {0: "batch"}
         lowerCamelCase			: List[str]   =    {0: "batch", 1: "past_decoder_sequence + sequence"}
       else:
         lowerCamelCase			: Optional[Any]   =    {0: "batch", 1: "decoder_sequence"}
         lowerCamelCase			: Tuple   =    {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			: str   =    OrderedDict(
           [
               ("input_ids", {0: "batch", 1: "encoder_sequence"}),
               ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
           ]			)
       if self.use_past:
         lowerCamelCase			: int   =    self.num_layers
         for i in range(__A			):
           lowerCamelCase			: Optional[int]   =    {0: "batch", 2: "past_sequence + sequence"}
           lowerCamelCase			: List[str]   =    {0: "batch", 2: "past_sequence + sequence"}
     else:
       lowerCamelCase			: 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			):






     """simple docstring"""





     if self.task in ["default", "seq2seq-lm"]:
       lowerCamelCase			: Any   =    super().outputs
     else:
       lowerCamelCase			: List[str]   =    super(__A						,    self			).outputs
       if self.use_past:
         lowerCamelCase			: Any   =    self.num_layers
         for i in range(__A			):
           lowerCamelCase			: Any   =    {0: "batch", 2: "past_sequence + sequence"}
           lowerCamelCase			: Tuple   =    {0: "batch", 2: "past_sequence + sequence"}
     return common_outputs






   def _snake_case   (		self						,    __A						,    __A = -1						,    __A = -1						,    __A = False						,    __A = None						,    ):






     """simple docstring"""





     lowerCamelCase			: Optional[int]   =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         __A						,    __A						,    __A						,    __A						,    __A			)

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

       lowerCamelCase			: Optional[int]   =    torch.cat(
           [common_inputs["decoder_attention_mask"], torch.ones(__A						,    __A			)]						,    dim=1			)

       lowerCamelCase			: Dict   =    []
       # If the number of encoder and decoder layers are present in the model configuration, both are considered
       lowerCamelCase			: List[str]   =    self.num_layers
       lowerCamelCase			: List[Any]   =    min(__A						,    __A			)
       lowerCamelCase			: List[Any]   =    max(__A						,    __A			) - min_num_layers
       lowerCamelCase			: List[str]   =    "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			: Union[str, Any]   =    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 _snake_case   (		self						,    __A						,    __A = -1						,    __A = -1						,    __A = False						,    __A = None						,    ):






     """simple docstring"""





     lowerCamelCase			: Dict   =    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			: int   =    common_inputs["input_ids"].shape
       # Not using the same length for past_key_values
       lowerCamelCase			: Optional[Any]   =    seqlen + 2
       lowerCamelCase			: Optional[Any]   =    self.num_layers
       lowerCamelCase			: Tuple   =    self.num_attention_heads
       lowerCamelCase			: Optional[Any]   =    (
           batch,
           num_encoder_attention_heads,
           past_key_values_length,
           self._config.hidden_size // num_encoder_attention_heads,
       )

       lowerCamelCase			: List[Any]   =    common_inputs["attention_mask"].dtype
       lowerCamelCase			: Tuple   =    torch.cat(
           [common_inputs["attention_mask"], torch.ones(__A						,    __A						,    dtype=__A			)]						,    dim=1			)
       lowerCamelCase			: List[Any]   =    [
           (torch.zeros(__A			), torch.zeros(__A			)) for _ in range(__A			)
       ]
     return common_inputs






   def _snake_case   (		self						,    __A						,    __A = -1						,    __A = -1						,    __A = False						,    __A = None						,    ):






     """simple docstring"""





     lowerCamelCase			: Optional[int]   =    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			: Union[str, Any]   =    tokenizer.num_special_tokens_to_add(__A			)
     lowerCamelCase			: List[Any]   =    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			: Optional[int]   =    [" ".join([tokenizer.unk_token]			) * seq_length] * batch_size
     lowerCamelCase			: Any   =    dict(tokenizer(__A						,    return_tensors=__A			)			)
     return common_inputs






   def _snake_case   (		self						,    __A						,    __A = -1						,    __A = -1						,    __A = False						,    __A = None						,    ):






     """simple docstring"""





     if self.task in ["default", "seq2seq-lm"]:
       lowerCamelCase			: 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			: Dict   =    self._generate_dummy_inputs_for_causal_lm(
           __A						,    batch_size=__A						,    seq_length=__A						,    is_pair=__A						,    framework=__A			)
     else:
       lowerCamelCase			: Union[str, 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 _snake_case   (		self						,    __A						,    __A						,    __A						,    __A			):






     """simple docstring"""





     if self.task in ["default", "seq2seq-lm"]:
       lowerCamelCase			: Tuple   =    super()._flatten_past_key_values_(__A						,    __A						,    __A						,    __A			)
     else:
       lowerCamelCase			: Optional[int]   =    super(__A						,    self			)._flatten_past_key_values_(
           __A						,    __A						,    __A						,    __A			)







import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
   from PIL import Image
class      UpperCAmelCase_ ( unittest.TestCase		,			UpperCamelCase   ):

   '''simple docstring'''




   def _snake_case   (		self			):






     """simple docstring"""





     lowerCamelCase			: List[Any]   =    load_tool("image-segmentation"			)
     self.tool.setup()
     lowerCamelCase			: Optional[Any]   =    load_tool("image-segmentation"						,    remote=__A			)






   def _snake_case   (		self			):






     """simple docstring"""





     lowerCamelCase			: List[str]   =    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
     lowerCamelCase			: Dict   =    self.tool(__A						,    "cat"			)
     self.assertTrue(isinstance(__A						,    Image.Image			)			)






   def _snake_case   (		self			):






     """simple docstring"""





     lowerCamelCase			: int   =    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
     lowerCamelCase			: int   =    self.remote_tool(__A						,    "cat"			)
     self.assertTrue(isinstance(__A						,    Image.Image			)			)






   def _snake_case   (		self			):






     """simple docstring"""





     lowerCamelCase			: Optional[int]   =    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
     lowerCamelCase			: Optional[Any]   =    self.tool(image=__A						,    label="cat"			)
     self.assertTrue(isinstance(__A						,    Image.Image			)			)






   def _snake_case   (		self			):






     """simple docstring"""





     lowerCamelCase			: str   =    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
     lowerCamelCase			: Dict   =    self.remote_tool(image=__A						,    label="cat"			)
     self.assertTrue(isinstance(__A						,    Image.Image			)			)



