

'''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 					lowerCAmelCase       ( UpperCAmelCase_				):
       lowerCAmelCase_				=	"bigbird_pegasus"
       lowerCAmelCase_				=	["past_key_values"]
       lowerCAmelCase_				=	{
           "num_attention_heads": "encoder_attention_heads",
           "hidden_size": "d_model",
           "attention_probs_dropout_prob": "attention_dropout",
       }







       def __init__(							self     :	int  , __lowercase     :	Dict=96103  , __lowercase     :	Optional[int]=4096  , __lowercase     :	Tuple=16  , __lowercase     :	Optional[int]=4096  , __lowercase     :	Dict=16  , __lowercase     :	str=16  , __lowercase     :	Dict=4096  , __lowercase     :	str=16  , __lowercase     :	int=0.0  , __lowercase     :	List[Any]=0.0  , __lowercase     :	Optional[int]=True  , __lowercase     :	List[str]=True  , __lowercase     :	List[Any]="gelu_new"  , __lowercase     :	Union[str, Any]=1024  , __lowercase     :	List[Any]=0.1  , __lowercase     :	Optional[Any]=0.0  , __lowercase     :	Union[str, Any]=0.0  , __lowercase     :	Optional[int]=0.0_2  , __lowercase     :	Dict=2  , __lowercase     :	Optional[int]=0.0  , __lowercase     :	List[Any]=True  , __lowercase     :	Any=0  , __lowercase     :	Union[str, Any]=2  , __lowercase     :	int=1  , __lowercase     :	Dict="block_sparse"  , __lowercase     :	Tuple=64  , __lowercase     :	Optional[Any]=3  , __lowercase     :	Tuple=False  , **__lowercase     :	str  , ):







            """simple docstring"""







            __lowercase     						=vocab_size
            __lowercase     						=max_position_embeddings
            __lowercase     						=d_model
            __lowercase     						=encoder_ffn_dim
            __lowercase     						=encoder_layers
            __lowercase     						=encoder_attention_heads
            __lowercase     						=decoder_ffn_dim
            __lowercase     						=decoder_layers
            __lowercase     						=decoder_attention_heads
            __lowercase     						=dropout
            __lowercase     						=attention_dropout
            __lowercase     						=activation_dropout
            __lowercase     						=activation_function
            __lowercase     						=init_std
            __lowercase     						=encoder_layerdrop
            __lowercase     						=decoder_layerdrop
            __lowercase     						=classifier_dropout
            __lowercase     						=use_cache
            __lowercase     						=encoder_layers
            __lowercase     						=scale_embedding  # scale factor will be sqrt(d_model) if True

            # extra config
            __lowercase     						=attention_type
            __lowercase     						=block_size
            __lowercase     						=num_random_blocks
            __lowercase     						=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 					lowerCAmelCase       ( UpperCAmelCase_				):







       @property
       def      snake_case				(							self     :	Dict ):







            """simple docstring"""







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

                 if self.use_past:
                      __lowercase     						={0: 'batch'}
                      __lowercase     						={0: 'batch', 1: 'past_decoder_sequence + sequence'}
                 else:
                      __lowercase     						={0: 'batch', 1: 'decoder_sequence'}
                      __lowercase     						={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.
                 __lowercase     						=OrderedDict(
                     [
                         ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                         ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                     ] )
                 if self.use_past:
                      __lowercase						,       __lowercase     						=self.num_layers
                      for i in range(__UpperCamelCase ):
                           __lowercase     						={0: 'batch', 2: 'past_sequence + sequence'}
                           __lowercase     						={0: 'batch', 2: 'past_sequence + sequence'}
            else:
                 __lowercase     						=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     :	Any ):







            """simple docstring"""







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







       def      snake_case				(							self     :	Union[str, Any]  , __lowercase     :	Union[str, Any]  , __lowercase     :	List[Any] = -1  , __lowercase     :	List[str] = -1  , __lowercase     :	Tuple = False  , __lowercase     :	List[Any] = None  , ):







            """simple docstring"""







            __lowercase     						=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase )

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

                 __lowercase     						=torch.cat(
                     [common_inputs['decoder_attention_mask'], torch.ones(__UpperCamelCase  , __UpperCamelCase )]  , dim=1 )

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







            """simple docstring"""







            __lowercase     						=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
                 __lowercase						,       __lowercase     						=common_inputs['input_ids'].shape
                 # Not using the same length for past_key_values
                 __lowercase     						=seqlen + 2
                 __lowercase						,       __lowercase     						=self.num_layers
                 __lowercase						,       __lowercase     						=self.num_attention_heads
                 __lowercase     						=(
                     batch,
                     num_encoder_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )

                 __lowercase     						=common_inputs['attention_mask'].dtype
                 __lowercase     						=torch.cat(
                     [common_inputs['attention_mask'], torch.ones(__UpperCamelCase  , __UpperCamelCase  , dtype=__UpperCamelCase )]  , dim=1 )
                 __lowercase     						=[
                     (torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase )) for _ in range(__UpperCamelCase )
                 ]
            return common_inputs







       def      snake_case				(							self     :	Union[str, Any]  , __lowercase     :	List[Any]  , __lowercase     :	int = -1  , __lowercase     :	Dict = -1  , __lowercase     :	Union[str, Any] = False  , __lowercase     :	str = None  , ):







            """simple docstring"""







            __lowercase     						=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
            __lowercase     						=tokenizer.num_special_tokens_to_add(__UpperCamelCase )
            __lowercase     						=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
            __lowercase     						=[' '.join([tokenizer.unk_token] ) * seq_length] * batch_size
            __lowercase     						=dict(tokenizer(__UpperCamelCase  , return_tensors=__UpperCamelCase ) )
            return common_inputs







       def      snake_case				(							self     :	Tuple  , __lowercase     :	Optional[int]  , __lowercase     :	List[Any] = -1  , __lowercase     :	Optional[int] = -1  , __lowercase     :	Union[str, Any] = False  , __lowercase     :	Optional[Any] = None  , ):







            """simple docstring"""







            if self.task in ["default", "seq2seq-lm"]:
                 __lowercase     						=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":
                 __lowercase     						=self._generate_dummy_inputs_for_causal_lm(
                     __UpperCamelCase  , batch_size=__UpperCamelCase  , seq_length=__UpperCamelCase  , is_pair=__UpperCamelCase  , framework=__UpperCamelCase )
            else:
                 __lowercase     						=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      snake_case				(							self     :	List[Any]  , __lowercase     :	Optional[int]  , __lowercase     :	str  , __lowercase     :	Optional[int]  , __lowercase     :	Any ):







            """simple docstring"""







            if self.task in ["default", "seq2seq-lm"]:
                 __lowercase     						=super()._flatten_past_key_values_(__UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase )
            else:
                 __lowercase     						=super(__UpperCamelCase  , self )._flatten_past_key_values_(
                     __UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase  , __UpperCamelCase )





'''simple docstring'''

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCAmelCase							=					logging.get_logger(__name__)


class 					lowerCAmelCase       ( A				):
       lowerCAmelCase_				=	["pixel_values"]







       def __init__(							self     :	List[Any]  , __lowercase     :	bool = True  , __lowercase     :	Optional[Dict[str, int]] = None  , __lowercase     :	PILImageResampling = PILImageResampling.BILINEAR  , __lowercase     :	bool = True  , __lowercase     :	Union[int, float] = 1 / 255  , __lowercase     :	bool = True  , __lowercase     :	Optional[Union[float, List[float]]] = None  , __lowercase     :	Optional[Union[float, List[float]]] = None  , **__lowercase     :	List[Any]  , ):







            """simple docstring"""







            super().__init__(**__lowercase )
            __lowercase     						=size if size is not None else {'height': 224, 'width': 224}
            __lowercase     						=get_size_dict(__lowercase )
            __lowercase     						=do_resize
            __lowercase     						=do_rescale
            __lowercase     						=do_normalize
            __lowercase     						=size
            __lowercase     						=resample
            __lowercase     						=rescale_factor
            __lowercase     						=image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
            __lowercase     						=image_std if image_std is not None else IMAGENET_STANDARD_STD







       def      snake_case				(							self     :	Dict  , __lowercase     :	np.ndarray  , __lowercase     :	Dict[str, int]  , __lowercase     :	PILImageResampling = PILImageResampling.BILINEAR  , __lowercase     :	Optional[Union[str, ChannelDimension]] = None  , **__lowercase     :	Union[str, Any]  , ):







            """simple docstring"""







            __lowercase     						=get_size_dict(__lowercase )
            if "height" not in size or "width" not in size:
                 raise ValueError(f'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}''' )
            return resize(
                __lowercase  , size=(size['height'], size['width'])  , resample=__lowercase  , data_format=__lowercase  , **__lowercase )







       def      snake_case				(							self     :	str  , __lowercase     :	np.ndarray  , __lowercase     :	float  , __lowercase     :	Optional[Union[str, ChannelDimension]] = None  , **__lowercase     :	Optional[Any] ):







            """simple docstring"""







            return rescale(__lowercase  , scale=__lowercase  , data_format=__lowercase  , **__lowercase )







       def      snake_case				(							self     :	Any  , __lowercase     :	np.ndarray  , __lowercase     :	Union[float, List[float]]  , __lowercase     :	Union[float, List[float]]  , __lowercase     :	Optional[Union[str, ChannelDimension]] = None  , **__lowercase     :	str  , ):







            """simple docstring"""







            return normalize(__lowercase  , mean=__lowercase  , std=__lowercase  , data_format=__lowercase  , **__lowercase )







       def      snake_case				(							self     :	str  , __lowercase     :	ImageInput  , __lowercase     :	Optional[bool] = None  , __lowercase     :	Dict[str, int] = None  , __lowercase     :	PILImageResampling = None  , __lowercase     :	Optional[bool] = None  , __lowercase     :	Optional[float] = None  , __lowercase     :	Optional[bool] = None  , __lowercase     :	Optional[Union[float, List[float]]] = None  , __lowercase     :	Optional[Union[float, List[float]]] = None  , __lowercase     :	Optional[Union[str, TensorType]] = None  , __lowercase     :	Union[str, ChannelDimension] = ChannelDimension.FIRST  , **__lowercase     :	Tuple  , ):







            """simple docstring"""







            __lowercase     						=do_resize if do_resize is not None else self.do_resize
            __lowercase     						=do_rescale if do_rescale is not None else self.do_rescale
            __lowercase     						=do_normalize if do_normalize is not None else self.do_normalize
            __lowercase     						=resample if resample is not None else self.resample
            __lowercase     						=rescale_factor if rescale_factor is not None else self.rescale_factor
            __lowercase     						=image_mean if image_mean is not None else self.image_mean
            __lowercase     						=image_std if image_std is not None else self.image_std

            __lowercase     						=size if size is not None else self.size
            __lowercase     						=get_size_dict(__lowercase )

            __lowercase     						=make_list_of_images(__lowercase )

            if not valid_images(__lowercase ):
                 raise ValueError(
                     'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                     'torch.Tensor, tf.Tensor or jax.ndarray.' )

            if do_resize and size is None:
                 raise ValueError('Size must be specified if do_resize is True.' )

            if do_rescale and rescale_factor is None:
                 raise ValueError('Rescale factor must be specified if do_rescale is True.' )

            # All transformations expect numpy arrays.
            __lowercase     						=[to_numpy_array(__lowercase ) for image in images]

            if do_resize:
                 __lowercase     						=[self.resize(image=__lowercase  , size=__lowercase  , resample=__lowercase ) for image in images]

            if do_rescale:
                 __lowercase     						=[self.rescale(image=__lowercase  , scale=__lowercase ) for image in images]

            if do_normalize:
                 __lowercase     						=[self.normalize(image=__lowercase  , mean=__lowercase  , std=__lowercase ) for image in images]

            __lowercase     						=[to_channel_dimension_format(__lowercase  , __lowercase ) for image in images]

            __lowercase     						={'pixel_values': images}
            return BatchFeature(data=__lowercase  , tensor_type=__lowercase )


